2011-10-31 17 views

回答

1

PHP中的多重選擇元素返回一個數組,所以它只是比較返回數組的count()與您想要的最大值如果計數超出限制允許併產生某種錯誤(拋出異常,觸發錯誤等)。

+0

我不能在客戶端做到這一點?喜歡使用一些javascript – Jewel

+1

是的,但你用PHP標記了你的問題,這是一種服務器端語言。如果你想要一個客戶端的答案,然後用javascript代替你的問題。 – GordonM

+2

即使使用客戶端驗證,您也應該始終進行服務器端驗證,因爲沒有任何措施可以阻止用戶關閉客戶端大小驗證。 – GordonM

1

如果你想在服務器端做到這一點,請看count()函數。例如,

HTML:

<form ...> 
    <select name="options[]" multiple="multiple"> 
    <!-- options here ... --> 
    </select> 
    ... 
</form> 

PHP:

<?php 

if (isset($_POST['options']) && count($_POST['options']) <= 3) { 
    // OK 
} else { 
    // Not OK 
} 

?> 
+0

不在服務器端。我想檢查客戶端。用戶不能一次選擇三個以上的項目 – Jewel

+1

@Saira PHP建議的服務器端。請務必閱讀GordonM對自己答案的評論。 – jensgram

3

下面是一個完整的例子,顯示了一個簡單的HTML表單和PHP只允許最多三個選項是選擇 - 以及額外的客戶端JavaScript獎勵,以解釋如何在提交表單之前向用戶通知錯誤。

您可以使用JavaScript來停止檢查三個以上選項的用戶,但請注意可以輕鬆避免客戶端驗證,因此您仍然需要使用PHP在服務器上進行驗證。


HTML

<!doctype html> 
<head> 
    <meta charset="utf-8"> 
    <title>Multiselect example</title> 
</head> 

<p>Please make a selection below (maximum three options!)</p> 

<form method="post"> 
    <select name="selection[]" multiple="multiple"> 
     <!-- Put <option> elements here --> 
    </select> 

    <input type="submit"> 
</form> 

<script src="Script.js"></script> 

的JavaScript(在的script.js):

var formChange = function(e) { 
    var i, 
     num = 0, 
     // Obtain a list of all selected options: 
     nodeList = this["selection[]"]; 

    // Loop over all options, count how many are selected. 
    for(i = 0; i < nodeList.length; i++) { 
     if(nodeList[i].selected) { 
      num ++; 
     } 
    } 

    if(num > 3) { 
     alert("Please do not select more than three options"); 
     e.preventDefault(); 
    } 
}; 

// Attach the same function to the change and submit event. 
// This will alert the user of more than three selections 
// as the fourth is selected, or as the form is submitted. 
// ... it will also not allow the form to submit with 
// more than three checked boxes. 
document.forms[0].addEventListener("change", formChange); 
document.forms[0].addEventListener("submit", formChange); 

PHP:

if(isset($_POST["selection"])) { 
    if(count($_POST["selection"]) > 3) { 

     die("Error: More than three check boxes were checked."); 
    } 

    // $_POST["selection"] is an array of the checked values. 
} 
0

看看這個js功能,假設你的多選對象「muSelect」

function checkSelected() { 
var i; 
    var count = 0; 
    for (i=0; i<muSelect.options.length; i++) { 
    if (muSelect.options[i].selected) { 
     count++; 
    } 
    if (count > 3) { 
     alert("cant select more than three options"; 
     // any other additionnal processing 
     } 
    } 
} 

,然後你可以添加此功能onClick事件

0

我知道這是一個老問題,但如果有人正在嘗試做同樣的事情,尋找答案是最好爲這個寫一個當前的答案。目前最好的辦法是用AJAX(只要我知道):

HTML:form.html

<form id="myForm" action="validate.php?q=1"> 
     <select name="options[]" multiple="multiple"> 
     <!-- options here ... --> 
     </select> 
     ... 
    </form> 

PHP:validate.php

<?php 
     switch($_REQUEST['q']){ 
      case 0: 
       if(isset($_REQUEST['options']) && count($_REQUEST['options']) <=3){ 
        echo true; 
       }else{ 
        echo false; 
       } 
      break; 
      case 1: 
       if(isset($_POST['options']) && !empty($_POST['options'])){ 
        if(count($_POST['options']) <= 3){ 
         //OK 
        }else{ 
         //NOT OK 
        } 
       } 
      break; 
     } 
    ?> 

的JavaScript(jQuery的): script.js

var form = $("#myForm"); 
    var options = $("#select_menu option") 
    var selected = []; 

    $(options).each(function(key,option){ 
     if(option.selected){ 
      selected[key] = option; 
     } 
    }); 

    $.ajax({ 
     url: "validate.php", 
     method: "GET", 
     data: { 
      options: selected, 
      q: 0 
     }, 
     success: function(Response){ 
      if(Response === true){ 
       //OK 
      }else{ 
       //NOT OK 
      } 
     } 
    }); 

請糾正我,如果我有錯誤的地方。