Php 如何检查复选框是否为空
如果这是一个愚蠢的问题,我道歉。我想检查一个或多个复选框是否为空,然后可以处理表单并打印电子邮件的兴趣文本Php 如何检查复选框是否为空,php,Php,如果这是一个愚蠢的问题,我道歉。我想检查一个或多个复选框是否为空,然后可以处理表单并打印电子邮件的兴趣文本 HTML/PHP <p><label for="commentsText">I am interested in:</label><br/> <input type="checkbox" name="interest[]" id="interest" value="1" class="required requiredField ema
HTML/PHP
<p><label for="commentsText">I am interested in:</label><br/>
<input type="checkbox" name="interest[]" id="interest" value="1" class="required requiredField email" />
Exhibiting. Please send me more information<br/>
<input type="checkbox" name="interest[]" id="interest" value="2" class="required requiredField email" />
Visiting. Please add me to your mailing list<br/>
<input type="checkbox" name="interest[]" id="interest" value="3" class="required requiredField email" />
Speaking. Please send me more information<br/>
<input type="checkbox" name="interest[]" id="interest" value="4" class="required requiredField email" />
Attending. Please send me more information<br/>
<?php if($interestError != '') { ?>
<span class="error"><?php echo $interestError; ?></span>
<?php } ?>
</p>
$interest='';
if(empty($_POST[$interest]))
{
$interestError ='Please select the interests';
$hasError = true;
}
else{
$numberofInterest = count($interest);
for ($i=0; $i < $numberofInterest; $i++)
{
$numofInterest = $interest[i];
echo $numofInterest . " ";
}
}
编辑#2
谢谢大家的帮助。我用了print_r,看到四个都打印出来了。现在的问题是:如果没有错误,就发送邮件。当我选中所有复选框时,它不会全部显示。仅显示“4”。是否有一种方法可以显示包括文本在内的所有内容
if(!isset($hasError)) {
$interest = $numofInterest ;
$subject = 'I Have A Question to Ask from '.$name;
$thanksubject = 'Thank you for the Form Submission';
$body = "Name: $name \n\nEmail: $email \n\nInterest: $interest\n\nComments: $comments";
*编辑#3*
好的,我已经解决了上面最后一个问题。设法发送电子邮件的一切 让我们做一个打印($\u POST);发现发生了什么。您将了解数据是如何传递的。。以及将来如何解决类似问题。提交表单时,PHP脚本将分别在
$\u GET
或$\u POST
变量中收到一组复选框(或者您可以使用)
即,当选中第一个和第三个复选框时,print\r($\u POST['interest'])代码>将输出:
Array
(
[0] => 1
[1] => 3
)
我认为你的主要错误是你索引$\u POST
-变量的方式-你使用变量$interest
在你应该使用字符串'interest'
的地方,如上所述。你可以在发布表单之前在javascript中检查这个,如