2015-11-25 21 views
0

我一直在試圖找到解決方案,爲此3天。希望有人能幫助。複選框需要將用戶限制爲2個或更多選擇。我知道如何用JavaScript做到這一點,但需要使用PHP。這是可能的,如果是這樣,如何?限制php所需的複選框數量

  <label><input type="checkbox" value="Lemon" name='filling[]' />Lemon</label></br> 
     <label><input type="checkbox" value="Custard" name='filling[]' />Custard</label></br> 
     <label><input type="checkbox" value="Fudge" name='filling[]' />Fudge</label></br> 
     <label><input type="checkbox" value="Mocha" name='filling[]' />Mocha</label></br> 
     <label><input type="checkbox" value="Raspberry" name='filling[]' />Raspberry</label></br> 
     <label><input type="checkbox" value="Pineapple" name='filling[]' />Pineapple</label> 
+1

php是一種服務器端語言。它可以**不**做任何事情來監視/限制某人在瀏覽器中做什麼。在表單提交給服務器後,最多可以檢查多少個複選框。 –

+0

對於瀏覽器中的這種限制,您需要使用JavaScript,計數輸入的填充時可能會引發PHP錯誤,但這將是荒謬的驗證和錯誤。 – Svetoslav

+0

爲什麼您需要使用PHP? – jeffdill2

回答

0

這兩個回答給了我一個好的開始。得到它與以下的PHP工作。

if(empty($_POST['filling']) || count($_POST['filling']) < 2) 
{ 
show_error("Please select at least two fillings"); 
} 
$filling = check_input(implode("," , $_POST['filling'])); 
0

您將通過計算選擇

if(count($_POST['filling']) < 2){ 
    // throw error 
} 
0

我假設你正在嘗試處理提交通過PHP的一種形式,並希望用戶選擇至少2個複選框的數量做到這一點。提交表單時,只有在複選框被選中時,複選框纔會發送到服務器。在你的設置中,你正在使用一個數組作爲名稱。只需檢查數組的大小是否至少爲2。

$checkboxes = $_POST['filling']; 

if(count($checkboxes) < 2) { 
    die("You must select at least 2 checkboxes."); 
} 

很明顯,你可以用這個做很多事情,但你得到的主意。