Solution to: How do I get PHP to understand form element arrays as encoded by jQuery’s .serialize()?
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):
0fa43a53e967f47e9c87136924df9442000
Then:
0fa43a53e967f47e9c87136924df9442001
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:
0fa43a53e967f47e9c87136924df9442002
Becomes this:
0fa43a53e967f47e9c87136924df9442003
And there you have it. Like I said, simple and stupid.