php 默认选中复选框 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14745308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default check a checkbox html
提问by myladeybugg
I am trying to get a checkbox checked by default, but everything I have tried doesn't seem to work. I don't know if it has to do with the PHP that is in the code.
我试图在默认情况下选中一个复选框,但我尝试过的一切似乎都不起作用。我不知道它是否与代码中的PHP有关。
function show_subscription_checkbox ($id='0') {
global $sg_subscribe;
sg_subscribe_start();
if ( $sg_subscribe->checkbox_shown ) return $id;
if ( !$email = $sg_subscribe->current_viewer_subscription_status() ) :
$checked_status = ( !empty($_COOKIE['subscribe_checkbox_'.COOKIEHASH]) && 'checked' == $_COOKIE['subscribe_checkbox_'.COOKIEHASH] ) ? true : false;
?>
<p <?php if ($sg_subscribe->clear_both) echo 'style="clear: both;" '; ?>class="subscribe-to-comments">
<input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" <?php if ( $checked_status ) echo 'checked="checked" '; ?>/>
<label for="subscribe"><?php echo $sg_subscribe->not_subscribed_text; ?></label>
</p>
This is a wordpress plugin that allows you to subscribe to blog comments.
这是一个 wordpress 插件,允许您订阅博客评论。
I have tried
我试过了
echo 'checked=\"checked\" ';
echo 'checked="checked" ' ;
echo 'checked> ';
The plugin author states that you used to be able to default check the checkbox but not anymore.
插件作者指出,您过去可以默认选中复选框,但现在不能了。
回答by EleventyOne
Since this is showing up in google for "default checked checkbox", I figured I'd answer it. Alpay was right: The correct way to ensure that a checkbox is checked by default is like so (followed by an example of one that is not checked):
由于这在谷歌中显示为“默认选中复选框”,我想我会回答它。Alpay 是对的:确保默认选中复选框的正确方法是这样的(接下来是未选中的示例):
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
Answer was found on w3schools. The author of the original question was having trouble with his PHP code, which is not at all related to the question title.
在w3schools 上找到了答案。原始问题的作者在处理他的 PHP 代码时遇到了问题,这与问题标题完全没有关系。
回答by Alpay
In HTML, if you want a checkbox to be checked by default, see the following;
在 HTML 中,如果您希望默认选中复选框,请参阅以下内容;
<input type="checkbox" name="name1" value="uc"> This checkbox is unchecked <br>
<input type="checkbox" name="name2" value="c" checked> This checkbox is checked<br>
So, you might consider changing
所以,你可以考虑改变
<?php if ( $checked_status ) echo 'checked="checked" '; ?>
to
到
<?php if ( $checked_status ) echo 'checked'; ?>
回答by Jukka K. Korpela
The problem is not in the HTML markup being generated; echo 'checked="checked" ', as in the question, works well, and so would the simpler echo 'checked'.
问题不在于生成的 HTML 标记;echo 'checked="checked" ',正如在问题中一样,效果很好,更简单的echo 'checked'.
The problem is with the condition $checked_status. You are testing for a variable that is undefined, as far as the code posted is considered.
问题出在条件上$checked_status。就考虑发布的代码而言,您正在测试未定义的变量。
回答by user4415474
I had the same problem. I figured out that I was trying to put the checkbox into a table but left out the and.
我有同样的问题。我发现我试图将复选框放入表格中,但忽略了和。
didn't check:
没有检查:
if(!$stump){echo '<input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b>';}
checked:
检查:
if(!$stump){echo '<td><input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b></td>';}
回答by Degar007
In regular PHP you can use this to "save" the checked state after its been submitted.
在常规 PHP 中,您可以使用它在提交后“保存”已检查状态。
<form name="checkbox" method="post" action="#" >
<input type="checkbox" name="checkbox1" value="Bike" <?php if ($_POST['checkbox1']=="Bike") echo "checked";?>>I have a bike
<input type="checkbox" name="checkbox2" value="Car" <?php if ($_POST['checkbox2']=="Car") echo "checked";?>>I have a car
<input type="submit" name="submit" value="Display this Data" />
if you want to use this data for something else after the submit, just add:
如果您想在提交后将此数据用于其他用途,只需添加:
<?php
if(isset($_POST['checkbox1']) OR isset($_POST['checkbox2']) )
{
echo "for 1 : ".$_POST['checkbox1']."for 2: ".$_POST['checkbox2'];
}
?>
if you want to clear the form (basically clear all the post data) you can add:
如果您想清除表单(基本上清除所有帖子数据),您可以添加:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="submit" name="Clear all form data" value= "Clear this form">
</form>

