复选框数组只返回一个结果

时间:2021-01-19 22:03:55

I have built a form that has a checkbox input array (saving to an array). However, when I POST it and retrieve the results, it only offers the last selection.

我已经构建了一个具有复选框输入数组(保存到数组中)的表单。但是,当我发布它并检索结果时,它只提供最后的选择。

<input type="checkbox" value="Friendly" name="quest[9]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[9]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[9]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[9]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[9]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[9]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>

For example, say I chose "Friendly", "Efficient", and "Genuine".

例如,假设我选择了“友好”、“高效”和“真诚”。

When I POST it over to a PHP document and run

当我将它发布到PHP文档并运行时

print_r($_POST['quest']);

I only receive

我只收到

Array ( [9] => Genuine )

back so "Genuine" is the only item I get back. Is there a way to fix this? What have I done wrong?

回来,所以“真正的”是我唯一得到的东西。有办法解决这个问题吗?我做错了什么?

This is the 9th question on the survey, so changing the name unfortunately is not an option. Is there any way to combine the results to that single array separated by commas? I could always explode on the php side.

这是调查中的第9个问题,因此不幸的是,改变名字是不可能的。有没有什么方法可以将结果与由逗号分隔的单个数组相结合?我总是会在php端爆发。

3 个解决方案

#1


2  

I'm posting a new answer about your comments on the previous one:

关于你对前一个问题的评论,我有一个新的回答:

Since you must keep quest[9] as the organization for the checkbox array..

因为您必须将quest[9]作为复选框数组的组织。

You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.

您可能想尝试使它成为一个更复杂的数组,其中每个都有name="quest[9][1]", name="quest[9][2]"等等。

And find the contents by

并找到内容

print_r($_POST['quest']);

again

再一次

#2


3  

All your checkboxes have the same name, make them unique

所有的复选框都有相同的名字,使它们独一无二

<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>

or use empty square brackets so php will treat the inputs as an array

或者使用空方括号,这样php就会把输入当作数组来处理

<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>

#3


3  

use quest[] in name instead of quest[9]. also in php part use this to add multiple choices .

使用任务[]代替任务[9]。在php部分,也可以使用它添加多个选项。

<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>

Happy coding!!

编码快乐! !

#1


2  

I'm posting a new answer about your comments on the previous one:

关于你对前一个问题的评论,我有一个新的回答:

Since you must keep quest[9] as the organization for the checkbox array..

因为您必须将quest[9]作为复选框数组的组织。

You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.

您可能想尝试使它成为一个更复杂的数组,其中每个都有name="quest[9][1]", name="quest[9][2]"等等。

And find the contents by

并找到内容

print_r($_POST['quest']);

again

再一次

#2


3  

All your checkboxes have the same name, make them unique

所有的复选框都有相同的名字,使它们独一无二

<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>

or use empty square brackets so php will treat the inputs as an array

或者使用空方括号,这样php就会把输入当作数组来处理

<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>

#3


3  

use quest[] in name instead of quest[9]. also in php part use this to add multiple choices .

使用任务[]代替任务[9]。在php部分,也可以使用它添加多个选项。

<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>

Happy coding!!

编码快乐! !