4

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

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.

3 Answers 3

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

<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>
Sign up to request clarification or add additional context in comments.

4 Comments

the problem is I am already posting to a survey this happens to be the 9th question on the survey so that isn't an option unfortunately, is there any way to combine the results to that single array separated by commas, I could always Explode on the php side
You should rename it quest9[] then
the problem then comes in recognizing how many answers exist, these are dynamic surveys each one may have anywhere from 3 to 500 questions(currently it runs a foreach on the single array to process all inputs)
What's the problem you can count it in the foreach or use the count function on the array
3

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

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

Happy coding!!

Comments

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..

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.

And find the contents by

print_r($_POST['quest']);

again

2 Comments

excellent, didnt know you could do that... that should do the trick thanks!
You can combine the two answers, and use quest[9][] as the names for all these checkboxes. PHP will then construct an array containing just the values of the checked items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.