【问题标题】:how to check checkboxes whether they are empty or not如何检查复选框是否为空
【发布时间】:2012-03-29 16:27:26
【问题描述】:

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

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*

好的,我已经解决了上面的最后一个问题。设法用一切发送电子邮件。

【问题讨论】:

标签: php


【解决方案1】:

提交表单后,PHP 脚本将分别收到 $_GET$_POST 变量中的复选框数组(或者您可以只使用 $_REQUEST

即检查第一个和第三个复选框时, print_r($_POST['interest']); Will输出:

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

我认为你的主要错误是你索引$_POST-变量的方式 - 你使用变量$interest,你应该使用字符串'interest',就像上面一样。

【讨论】:

    【解决方案2】:

    让我们做一个 print_r($_POST);并发现正在发生的事情。您将了解数据是如何传递的……以及将来如何解决类似问题。

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多