2013-08-03 64 views
0

在我的形式,我有這樣的一部分:如何提交複選框的值?

<input type="checkbox" name="city" value="Nicosia" class="choosecity">Nicosia<br> 
<input type="checkbox" name="city" value="Limassol" class="choosecity">Limassol<br> 
<input type="checkbox" name="city" value="Larnaca" class="choosecity">Larnaca<br> 

,並在那裏我使用郵件功能在結果頁面上,我想thechecked城市。

我用這個無果而終:

缺少什麼我在這裏?

+1

您的表單名需要utlize數組語法然後,使用'<輸入名稱=「city []」>' – mario

回答

1

您需要命名您的輸入作爲一個數組name="city[]"

2

使用name="city[]"。否則,您只能提交一個城市。您可能還需要使用

$cities = isset($_POST['city']) ? $_POST['city'] : array(); 
foreach ($cities as $city) 
1

PHP使用方括號語法形式的輸入轉換成一個數組,所以當你使用的名稱=當你做這個「教育[]」你會得到一個數組:

$educationValues = $_POST['education']; // Returns an array 
print_r($educationValues); // Shows you all the values in the array 

因此,例如:

<p><label>Please enter your most recent education<br> 
    <input type="text" name="education[]"></p> 
<p><label>Please enter any previous education<br> 
    <input type="text" name="education[]"></p> 
<p><label>Please enter any previous education<br> 
    <input type="text" name="education[]"></p> 

會給你$ _ POST [ '教育']數組內的所有輸入的值。

在JavaScript中,這是更有效的獲得由ID的元素...

document.getElementById("education1"); 

ID不具有匹配名稱:

<p><label>Please enter your most recent education<br> 
    <input type="text" name="education[]" id="education1"></p> 
0

你剛纔只需要將此[]添加到輸入名稱,這將創建一個從[0]開始的數組。結果看起來如此:

array(
    [0] => 'Nicosia', 
    [1] => 'Limassol', 
    [2] => 'Larnaca', 
) 

的HTML:

<input type="checkbox" name="city[]" value="Nicosia" class="choosecity" />Nicosia<br> 
<input type="checkbox" name="city[]" value="Limassol" class="choosecity" />Limassol<br> 
<input type="checkbox" name="city[]" value="Larnaca" class="choosecity" />Larnaca<br> 

的PHP:

if(isset($_POST[city]) && is_array($_POST[city])){ 
    foreach($_POST[city] as $checkbox){ 
     echo $checkbox . ' '; 
    } 
}