2012-03-28 23 views
-1

如果是愚蠢的问题,我很抱歉。我想检查一个或多个复选框是否为空,然后可以处理表单并打印电子邮件的兴趣文本。如何检查复选框是否为空

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我已经解决了上面最后一个。设法发送一切的电子邮件。

+0

你可以在发布表单之前在javascript中检查它,就像http://stackoverflow.com/questions/2684434/jquery-check-if-atleast-one-checkbox-is-checked – 2012-03-28 06:31:03

回答

0

当表单提交的PHP脚本将分别获得过的框的数组中的$_GET$_POST变量(或者你可以只使用$_REQUEST

即当第一和第三复选框被选中,一个print_r($_POST['interest']);将输出:

Array 
(
    [0] => 1 
    [1] => 3 
) 

我觉得你的主要错误,是你的方式指数$_POST -variable - 您使用的变量$interest,你应该使用字符串'interest' ,就像上面一样。

0

让我们做一个print_r($ _ POST);并发现发生了什么。您将了解数据如何传递......以及将来如何解决类似问题。

相关问题