Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array posting in PHP

Tags:

arrays

post

php

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!

like image 259
user45344 Avatar asked Jan 09 '09 19:01

user45344


People also ask

Can I POST array in PHP?

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.

What is $_ POST [] in PHP?

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.

What are the 3 types of PHP arrays?

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.

Is $_ POST an associative array?

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


1 Answers

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']...)

like image 97
David Avatar answered Oct 28 '22 08:10

David