我有一个带有几个复选框(不是组)的PHP表单,这些复选框具有不同的名称,在提交时会被发送到阵列并通过电子邮件发送。我遇到的问题是我正在寻找一种方法来验证提交时是否至少检查了其中一个复选框。我知道如何做一个复选框或做复选框组但不是这个。如何才能做到这一点?如果有人能指出我正确的方向,我将不胜感激。
我正在为我要求的表单部分添加代码。
<form action="" method="post">
<fieldset>
<legend>
Please Select at least one:
</legend>
<p class="style5">Type of Alleged Occurrence:<input name="occurrence" type="hidden" value="<?php { print 'Type of Occurrence'; }?>"></p>
<font size="-1" face="Arial, Helvetica, sans-serif" align="left">
<table style="width: 100%">
<tr><td style="width: 21%; height: 57px;"><strong> Disruption:</strong><br><input name="obscene" type="checkbox" value="Obscene Language" <?php if(isset($_POST['obscene'])) echo "checked"; ?>><label id="obscene">Obscene language </label>
</td><td style="width: 33%; height: 57px;">
<strong> Sexual Harassment:</strong><br><input name="sexharass" type="checkbox" value="Physical" <?php if(isset($_POST['sexharass'])) echo "checked"; ?> ><label id="sexharass">Physical </label> <span lang="en-us">
</span> <input name="sexharass" type="checkbox" value="Verbal" <?php if(isset($_POST['sexharass'])) echo "checked"; ?> ><label id="sexharass2">Verbal </label>
<td style="width: 21%; height: 57px;"><strong> Altercation:</strong>
<br><input name="altercation1" type="checkbox" value="Verbal" <?php if(isset($_POST['altercation1'])) echo "checked"; ?>><label id="Label3">Verbal</label>
<input name="altercation1" type="checkbox" value="Physical" <?php if(isset($_POST['altercation1'])) echo "checked"; ?> ><label id="Label4">Physical</label> <br>
<td style="width: 33%; height: 57px;"><strong>Involved in altercation:</strong><br>
<input name="altercation2" type="checkbox" value="student/student" <?php if(isset($_POST['altercation2'])) echo "checked"; ?>><label id="Label3">Student/Student </label>
<input name="altercation2" type="checkbox" value="student/faculty-staff" <?php if(isset($_POST['altercation2'])) echo "checked"; ?>><label id="Label4">Student/Faculty-Staff </label>
<tr><td><strong> Theft/ Damage to Property:</strong><br><input name="property" type="checkbox" value="DACC" <?php if(isset($_POST['property'])) echo "checked"; ?> ><label id="property">DACC </label> <span lang="en-us"> <font size="-1" face="Arial, Helvetica, sans-serif" align="left"><input name="property1" type="checkbox" value="Self" <?php if(isset($_POST['property'])) echo "checked"; ?> ><label id="property3">Self </label>
</font> <br>
</span><input name="property" type="checkbox" value="Faculty/Staff" <?php if(isset($_POST['property'])) echo "checked"; ?> ><label id="property2">Faculty/Staff </label>
</td>
<td><strong> Threat of Harm to Self or Others:</strong><br><input name="harm" type="checkbox" value="Student/Student" <?php if(isset($_POST['harm'])) echo "checked"; ?> >
<label id="harm">Student/Student </label> <span lang="en-us"> <font size="-1" face="Arial, Helvetica, sans-serif" align="left"><input name="harm2" type="checkbox" value="Self" <?php if(isset($_POST['harm'])) echo "checked"; ?> ><label id="property2">Self </label>
</font><br>
</span>
<font size="-1" face="Arial, Helvetica, sans-serif" align="left">
<span lang="en-us"><input name="harm1" type="checkbox" value="Student/Faculty-Staff" <?php if(isset($_POST['harm'])) echo "checked"; ?> ><label id="harm3">Student/Faculty-Staff </label>
</span></font></td></tr>
<tr><td><strong> Drugs/Alcohol:</strong><br><input name="drugs" type="checkbox" value="Under the Influence" <?php if(isset($_POST['drugs'])) echo "checked"; ?> ><label id="drugs">Under the Influence </label> <font size="-1" face="Arial, Helvetica, sans-serif" align="left"><input name="drugs" type="checkbox" value="Possession" <?php if(isset($_POST['drugs'])) echo "checked"; ?> ><label id="drugs2">Possession </label>
</font>
</td><td><strong> Other Occurrences:</strong><br><input name="other" type="checkbox" value="Student/Student" <?php if(isset($_POST['other'])) echo "checked"; ?> ><label id="other">Trespassing </label> <span lang="en-us"> <font size="-1" face="Arial, Helvetica, sans-serif" align="left"><input name="other" type="checkbox" value="Other" <?php if(isset($_POST['other'])) echo "checked"; ?> ><label id="other2">Other </label>
</font><br>
</span>
</td></tr> </table><p></p>
答案 0 :(得分:1)
根据您的表单结构,您需要使用isset()
检查。假设您不以数组(<input type='checkbox' name='cb[]' value='x' /><input type='checkbox' name='cb[]' value='x' />...etc
)发布,您可以这样做:
<form method='post'...>
<input type='checkbox' name='cb1' value='x' />
<input type='checkbox' name='cb2' value='x' />
</form>
验证......
$require_one_of = array('cb1','cb2',...etc); //names of posted checkboxes
$one_set=false;
foreach($require_one_of as $key){
if(isset($_POST[$key])){
$one_set=true;
break;
}
}
if(!$one_set){
//error handling
}
如果您要作为数组发布,那么您只需检查数组是否已设置:
<form method='post'...>
<input type='checkbox' name='cb[]' value='x' />
<input type='checkbox' name='cb[]' value='x' />
</form>
使用
进行验证<?php
if(!isset($_POST['cb'])){
//error handling
}
?>
注意:我在这里假设您使用帖子作为表单提交方法