Home Web Development jQuery Solution to: How do I get PHP to understand form element arrays...

Get social!

You could say that this “solution” is simple and I am stupid (and you might be right on both counts). Still, I thought I would post it anyway so that someone might save the 30 minutes it took me to figure it out.

Whilst creating a jQuery .load() function that used .serialize() to send data to a PHP script, I had some issues with multiple choice checkboxes (only the relevant code is displayed here):

Then:

The PHP script would only pick up the value for the last checked checkbox. So if I checked all the checkboxes, for instance, <?php echo $_GET['features']; ?> would display “Wireless Internet”, rather than “Array()”. PHP should see the same-named checkboxes as an array. right?

Solution

PHP requires the array delimiters ([]) to follow a variable name for two or more form elements with the same name (yes, I knew this!). So this:

Becomes this:

And there you have it. Like I said, simple and stupid.

Leave a Reply

Bad Behavior has blocked 24 access attempts in the last 7 days.