0

Hi and thanks for reading my question. I am using a simple form to get some input :

<p>Select your favorite two countries below:</p>

<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries" value="USA" /> USA<br />
<input type="checkbox" name="countries" value="Canada" /> Canada<br />
<input type="checkbox" name="countries" value="Japan" /> Japan<br />
<input type="checkbox" name="countries" value="China" /> China<br />
<input type="checkbox" name="countries" value="France" /> France<br />
<input type="submit" value="Order">
</form>

I want to make sure order.php is geting all of the choices selected, so order.php only contains the following code :

<pre>
<?php var_dump($_POST);?>
</pre>

Unfortunately, it is only outputting whatevre is the bottom-most checkbox that is checked.

The output is like this :

array(1) { ["countries"]=> string(6) "Canada" }

If i try the following code for output :

<?php
    foreach($_POST as $key=>$post_data){
        echo "You posted:" . $key . " = " . $post_data . "<br>";
    }
?>

I get this output :

You posted:countries = Canada

Can anyone tell me where i am going wrong and how i can retrieve all of the data, for every box that is ticked ?

Thank you.

2 Answers2

1

You gave the same name to your checkboxes, and PHP will overwrite previously parsed name submissions with the current value. You need to use the array-notation hack:

<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
                                      ^^

which then makes $_POST['countries'] an array of all the values submitted.

echo "You posted: " . implode(',', $_POST['countries']);
Marc B
  • 356,200
  • 43
  • 426
  • 500
0
<p>Select your favorite two countries below:</p>

<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries[]" value="USA" /> USA<br />
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
<input type="checkbox" name="countries[]" value="Japan" /> Japan<br />
<input type="checkbox" name="countries[]" value="China" /> China<br />
<input type="checkbox" name="countries[]" value="France" /> France<br />
<input type="submit" value="Order">
</form>

Change it to above, this will store all your checkboxes results for you!

baao
  • 71,625
  • 17
  • 143
  • 203
  • Thank you Michael and Mark B. Unfortunately, using the array notation, the script i am using to check that only a certain number of boxes are ticked will not work. The script is called checkboxlimit and is called like this : – user3475934 Oct 27 '14 at 17:13
  • 1
    sure it will. `if (count($_POST['countries']) > 5) { die("Too many"); }`. javascript won't care about the `[]`. it's just some extra characters in the name. – Marc B Oct 27 '14 at 17:14
  • This is the script that checks the limits, owuld i be able to change all references to checkgroup to an array ? I've never learned JS : 'function checkboxlimit(checkgroup, limit){ var checkgroup=checkgroup var limit=limit for (var i=0; ilimit){ alert("You can only select a maximum of "+limit+" checkboxes") this.checked=false } } } }' – user3475934 Oct 27 '14 at 17:17
  • Thanks Mark B. That line of code is PHP to check the limits is it? If so then i don't need the JS ? The JS doesn't work now that i've changed the form to array notation. – user3475934 Oct 27 '14 at 17:51
  • You can check it with this line on the nextpage (order.php in your case) – baao Oct 27 '14 at 17:57