如果选中复选框,则使用 PHP 更改值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11069090/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:42:01  来源:igfitidea点击:

if checkbox is checked change the value using PHP

phpforms

提问by Ali

I'm trying to accomplish one simple task (but is not simple for me).

我正在尝试完成一项简单的任务(但对我来说并不简单)。

I have a form and I'm having a trouble with this check box.

我有一个表单,但我在使用此复选框时遇到了问题。

    <input type="checkbox" name="b"  
<?php if (isset($_POST[b])) echo "value='y'"; else echo "value='n'"; ?>/>

I'm not sure if I use the right one, but it doesn't work for me.

我不确定我是否使用了正确的方法,但它对我不起作用。

So basically I want the value of the inputof bwill be yif the checkbox is checked else it will always be nif the checkbox is unchecked.

所以基本上我想要的价值inputb将是y,如果复选框被选中否则它永远是n,如果该复选框处于未选中状态。

回答by sachleen

That's not how a checkbox works.

这不是复选框的工作方式。

It's checked when the checkedattribute is there.

checked属性存在时它会被检查。

<input type="checkbox" name="a" value="a" checked /> Checked
<input type="checkbox" name="a" value="a" /> NOT Checked

So you want to use

所以你想用

<input type="checkbox" name="a" value="a" <?php echo isset($_POST['b']) ? "checked" : ""; ?>/>

Now if $_POST['b']is set, the checkbox will be checked.

现在如果$_POST['b']设置,复选框将被选中。

Also, you have $_POST[b]. The bshould be in quotes. It should be $_POST['b']

此外,您有$_POST[b]. 本b应在引号。它应该是$_POST['b']

回答by Ravi Kant Mishra

You have to use two conditions one is for showing checked/unchecked and second for showing y/n

您必须使用两个条件,一个是显示选中/未选中,第二个是显示 y/n

 <input type="checkbox" name="b" <?php echo (isset($_POST['b'])?"value='y'":"value='n'")?> 
 <?php echo (isset($_POST['b'])?"checked":"") ?>  />

回答by user1461434

tested code!

测试代码!

<form method="post">
   <input type="checkbox" name="b" <?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>/>
   <input type="submit" name="asd" value="asd">
</form>

So go with the following

因此,请执行以下操作

<?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>

回答by Justin Archiquette

This worked for me:
    first: Giving the checkbox a default value
    then: assign the desired value only if the box is checked.

这对我     有用:
    首先:为复选框提供默认值
然后:仅在选中该框时分配所需的值。

    <input type="hidden" name="b" value="n">
    <input type="checkbox" name="b" value="y" >

回答by Zetty 0905

*Approved by Deployment:

*经部署批准:

        <input type="radio" name="dep_approval_status" value="Approved"
            <?php 
                if ($deploy['dep_approval_status'] === "Approved")
                {
                    echo ' checked'; 
                }

            ?> 
        /> Yes, approved 

        <input type="radio" name="dep_approval_status" value="Not Approved"
            <?php 
                if ($deploy['dep_approval_status'] === "Not Approved")
                {
                    echo ' checked';
                }
            ?> 
        /> Not Approved