2017-03-03 130 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