如何使复选框状态不总是传递给PHP脚本? [英] How come checkbox state is not always passed along to PHP script?

查看:92
本文介绍了如何使复选框状态不总是传递给PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表单:

 < form action ='process.php'method ='post'> 
< input type ='checkbox'name ='check_box_1'/>请检查我!< br>
< / form>

这里是一个来自PHP脚本的部分 process.php

  echo(isset($ _ POST ['check_box_1'))?'Set' ; 

复选框设置时脚本的输出为



p>


未设置


这看起来是一个很糟糕的设计,因为我的PHP脚本检查了一些 $ _ POST 变量,以确保它们被传递到脚本。当 $ _ POST ['check_box_1'] 值未设置时,我如何知道脚本是否无法传递值或复选框是否未设置? / p>

解决方案

如果您想克服此设计功能,请尝试:

 < input type =hiddenname =check_box_1value =0/> 
< input type =checkboxname =check_box_1value =1/>

这样,你总是有 $ _ POST ['check_box_1'] 在回调页面中设置,然后你可以检查它的值,看看他们是否检查。两个输入必须是这样的顺序,因为后面的输入将优先。


I have an HTML form:

<form action='process.php' method='post'>
  <input type='checkbox' name='check_box_1' /> Check me!<br>
</form>

Here is a section from the PHP script process.php:

echo (isset($_POST['check_box_1']))?'Set':'Not set';

The output of the script when the checkbox is set is

Set

But when the checkbox is not set, the output is:

Not set

Why is this? This seems like a very poor design because my PHP script checks a number of $_POST variables to make sure they were passed along to the script. When the $_POST['check_box_1'] value is not set, then how do I know whether the script failed to pass along the value or the checkbox was just not set?

解决方案

If you want to overcome this design feature, try this:

<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />

This way, you'll always have $_POST['check_box_1'] set in the callback page, and then you can just check its value to see whether they checked it or not. The two inputs have to be in that order though, since the later one will take precedence.

这篇关于如何使复选框状态不总是传递给PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆