2017-03-03 40 views
1

謝謝你看我的問題。將表單中的複選框數組傳遞給PHP變量

我試圖做到這一點: 使用複選框的形式:用戶點擊幾個複選框(對於一個問題)。 然後點擊「提交」 然後一個PHP頁面接受表單元素並將它們放入變量中。我可以用其他數據做到這一點,但不知道如何用複選框(因爲它是一個數組)。

你能幫我嗎?

$Q1 = $_POST['Hood']; // This one is the array. Let's say it's an array that holds 3 words (red, white, blue). I'm fine if they all get stored in $Q1, I just don't know how to loop through the array (in $POST['Hood']) to get all 3 words in that one variable ($Q1) 

$Q2 = $_POST['Handle']; 
$Q3 = $_POST['Lever']; 

所以我想我需要找出如何通過$ _ POST [「罩」]循環才能得到每個數組元素出來。請幫忙:)

謝謝您的協助!

回答

2

這裏是你的表單輸入必須看起來像PHP和處理部分。注意輸入名稱有一個數組初始化:

echo '<form method="post"> 
<input type="checkbox" name="Hood[]" value="some value"> some value<br> 
<input type="checkbox" name="Hood[]" value="some other value"> some other value<br> 
<input type="checkbox" name="Hood[]" value="1"> a number<br> 
<button type="submit">Submit</button> 
</form>'; 

if(isset($_POST['Hood'])) { 

    foreach($_POST['Hood'] as $key=>$value) { 

     echo $value; 

    } 

} 
+0

太好了,我現在就試試吧! – Andrea

+0

完美運作。非常感謝! – Andrea