如果是愚蠢的問題,我很抱歉。我想檢查一個或多個複選框是否爲空,然後可以處理表單並打印電子郵件的興趣文本。如何檢查複選框是否爲空
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