php 未定义索引如果我不选中复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18603884/
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
Undefined Index If I Do Not Check Checkbox
提问by user577111
I seem to have a bizarre happening here. If I check a checkbox on the form then the php script runs perfectly. If I don't check the box php reports an undefined index on another variable.
我似乎在这里发生了一件奇怪的事情。如果我选中表单上的复选框,则 php 脚本运行良好。如果我不选中复选框 php 会报告另一个变量的未定义索引。
That's using IIS localhost, checking things.
那是使用 IIS 本地主机,检查事情。
On the web the published identical script works no matter what. Well, virtually identical. I have locally added a variable 'test' POST-ed to php and compared with a hard coded value. That's all.
在网络上,发布的相同脚本无论如何都有效。嗯,几乎一模一样。我在本地添加了一个变量 'test' POST-ed 到 php 并与硬编码值进行比较。就这样。
Here's the html for the checkbox:
这是复选框的html:
<tr>
<td>Publish Comment?</td>
<td><input name="publishok" value="NO" type="checkbox"> Check Box For Do spanstyle="font-weight: bold;">Not</span> Publish</td>
</tr>
<tr>
and here's the php for the variable, 'publishok' :
这是变量 'publishok' 的 php:
$IP = $_SERVER["REMOTE_ADDR"];
$publishok = $_POST['publishok'];
$test = $_POST['test'];
if ($test != 'park') die ("Wrong input. Sorry. Go back, try again");
I suspected my editor PSPad was adding spurious (and invisible) char codes or something so I upgraded to the latest version. No difference.
我怀疑我的编辑器 PSPad 正在添加虚假(和不可见)字符代码或其他东西,所以我升级到最新版本。没有不同。
Can't think what could cause this.
想不出是什么原因造成的。
Can anyone help?
任何人都可以帮忙吗?
回答by xdazz
checkbox won't send the data to the server when you did not check it.
如果您没有选中复选框,则不会将数据发送到服务器。
You have to use isset($_POST['publishok'])
to check it whether it is checked in the server side.
您必须使用isset($_POST['publishok'])
它来检查它是否在服务器端检查。
回答by totymedli
This happens because the checkbox's data isn't sent to the server if it is unchecked. A little hack for this is to use a hidden input field with the same name before the checkbox, so if the checkbox is unchecked, that value will be sent instead.
发生这种情况是因为如果未选中复选框的数据不会发送到服务器。一个小技巧是在复选框之前使用一个具有相同名称的隐藏输入字段,因此如果未选中复选框,则将发送该值。
<input name="publishok" value="0" type="hidden">
<input name="publishok" value="NO" type="checkbox">
回答by Class
I believe checkboxes and radio buttons don't send a get/post data if not selected/checked (you can verify this by doing a var_dump/print_r on $_GET/$_POST) so you should do something like:
我相信如果未选中/选中复选框和单选按钮不会发送获取/发布数据(您可以通过在 $_GET/$_POST 上执行 var_dump/print_r 来验证这一点),因此您应该执行以下操作:
if(isset($_POST['publishok'])){
$publishok = $_POST['publishok'];
}else{
$publishok = "";#default value
}
回答by Kwed
<input class="form-check-input" type="hidden" id="" name="check_box" value="0">
<input class="form-check-input" type="checkbox" id="gridCheck" name="check_box" value="1"
<?php if ($_POST['check_box'] == "1") {
print "checked";
} ?>>
回答by Ateeq Rafeeq
$_POST['wc_use_taxes'] = !isset($_POST['wc_use_taxes'])? "" : $_POST['wc_use_taxes'];
This code would set empty value in posted variable which isn't set and in case its set, that would make it equal to what posted. So i guess doing this would be good idea.
此代码将在未设置的已发布变量中设置空值,如果已设置,则将使其等于已发布的值。所以我想这样做是个好主意。