如果是愚蠢的问题,我很抱歉。我想检查一个或多个复选框是否为空,然后可以处理表单并打印电子邮件的兴趣文本。如何检查复选框是否为空
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>
PHP
$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 *
OK我已经解决了上面最后一个。设法发送一切的电子邮件。
你可以在发布表单之前在javascript中检查它,就像http://stackoverflow.com/questions/2684434/jquery-check-if-atleast-one-checkbox-is-checked – 2012-03-28 06:31:03