Question

I am trying to post an array full of checkboxes and to open it in the next page..

It only gives me the last result, anyone know why? or how to fix it?

<form name="input" action="createevent.php" method="post">

Event title: 
<input type="text" name="Eventtitle" size="20">
<br>Event Description 
<input type="text" name="Description" size="20">
<br>
Please select the days that you are free to arrange this meeting.<br>
Monday
<input type="checkbox" name="day" value="Monday" />
<br />
Tuesday
<input type="checkbox" name="day" value="Tuesday" />
<br />
Wednesday
<input type="checkbox" name="day" value="Wednesday" />
<br />
Thursday
<input type="checkbox" name="day" value="Thursday" />
<br />
Friday
<input type="checkbox" name="day" value="Friday" />
<br />
Saturday
<input type="checkbox" name="day" value="Saturday" />
<br />
Sunday
<input type="checkbox" name="day" value="Sunday" />
<br /><br />
<input type="submit" value="Submit">

and no matter how many you select it only gives a single result on the next page. $day = sizeof($_POST['day']);

only ever gives '1' answer. And when I get them to the next page I will want to be able to select them separately.

Thanks!

Was it helpful?

Solution

PHP will only automatically make a POST value into an array if it ends in [].

So you need to have name="day[]" instead of just name="day".

(Note that this works for any POST value, and also with associative arrays instead of just auto-incrementing -- you can do name="foo[bar]", and you'd get $_POST['foo']['bar']...)

OTHER TIPS

The reason you are only getting one result is because you are posting multiple fields with the same name so by default the last one overwrites all the previous ones. Try creating an array in your HTML like this:

Monday
<input type="checkbox" name="day[]" value="Monday" />
<br />
Tuesday
<input type="checkbox" name="day[]" value="Tuesday" />
<br />
Wednesday
<input type="checkbox" name="day[]" value="Wednesday" />

and so on down the list...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top