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!
Using $_POST array in PHP To use the Post method, we definitely need a form in our HTML file to handle data we want to post. The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.
PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.
In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.
$_POST is a predefined variable which is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.
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']
...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With