2012-01-31 53 views
1

我有一個帶有不同名稱的多個複選框(不是組)的不同名稱的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>&nbsp;<span lang="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </span>&nbsp;<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>&nbsp;<span lang="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;<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>&nbsp;<span lang="en-us">&nbsp;&nbsp;&nbsp;<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> 



      &nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;<span lang="en-us">&nbsp;&nbsp;&nbsp;<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

如果您發佈的形式是我使用後 – Sabari 2012-01-31 17:06:45

回答

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 
} 
?> 

注:我在此假設您正在使用後作爲表單提交方法

+0

爲形式的方法將是有益的。謝謝本,我會試試這個! – Inky1231 2012-02-01 15:17:33

+0

好的我添加了你的建議給我的代碼,即使沒有選中複選框,它也不會拋出任何錯誤,你能看看我做了什麼,你給我看看我做錯了嗎? 'code' \t $ require_one_of = array('obscene','altercation1','altercation2','sexharass','property','harm','drugs','other',); //發佈複選框的名稱 $ one_set = false; ($ set_one_of as $ key){ if(isset($ _POST [$ key])){ $ one_set = true; 休息; } } if(!$ one_set){echo'請選擇一種類型的發生'; //錯誤處理 } 'code' – Inky1231 2012-02-01 15:44:41

+0

有幾個問題:1)你的數組定義中有尾隨逗號...應該是數組('obscene','altercation1','altercation2','sexharass ','property','harm','drugs','other');'2)你的頁面中的代碼在哪裏?你確定沒有任何邏輯可以阻止循環進入......你可以發佈你的整個代碼嗎?另外,在接收頁面的底部添加'print_r($ _ POST)',以確保它看起來像你期望的 – 2012-02-01 17:30:18

相關問題