请注意,我刚刚开始学习 php。我已经学习了几个不同的多日教程,现在我正在尝试自己编写一个小的邮件列表脚本。我敢肯定,这是一个非常简单的问题/答案,但我没有想出如何表达这个问题的方式在谷歌上出现了任何结果。
我有一个名为“htemail”的复选框,当我将它发送到脚本时,我有以下代码:
if(!$_POST['htemail']){
$usehtml = 0;
}else{
$usehtml = 1;
}
echo($usehtml);
Run Code Online (Sandbox Code Playgroud)
如果选中该复选框,则返回 1,但如果未选中,则会出现以下错误:
注意:未定义索引:htemail in C:\wamp\www\mailing list\createUser.php on line 7 Call Stack #TimeMemoryFunctionLocation 10.0004673576{main}()..\createUser.php:0 0.
我想做的第一件事是向后翻转代码,以便它首先检查检查是否存在(我不确定为什么我认为这会有所帮助)
提前感谢您的任何答案。
如果未选中,浏览器可能不会包含在发布数据中。使用该功能对其isset进行检查。如果未设置,则表示未检查,反之则不为真,如果设置,则可能仍为假。
if(isset($_POST['htemail']) && $_POST['htemail'] == "Value"){ //where "Value" is the
//same string given in the HTML form, as value attribute the the checkbox input
$usehtml = 1;
}else{
$usehtml = 0;
}
Run Code Online (Sandbox Code Playgroud)
作为替代,isset您可以使用array_key_exists