0

This is a really strange problem. I've got a form with a couple of fields, of which one of them is a multi select that looks like this:

<select name="age_group[]" multiple="multiple">
  // options
</select>

When posting to the server, the key of the multi select in the $_POST array looks like this:

age_group[

Why did the closing bracket got stripped? Also, the field is not saved as an array, but a string for some reason. When console.log() the object I send to the server with jQuery, it all looks okay, and that very field is indeed an array.

POST header:

POST /ajax.php HTTP/1.1
Host: ds.local
Connection: keep-alive
Content-Length: 188
Accept: application/json, text/javascript, /; q=0.01
Origin: http://ds.local
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) > Chrome/29.0.1547.66 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://ds.local/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: PHPSESSID=9bniv5i558oslo6ne4ev59hdp6

POST data, human-readable

action: search
entity: DrugStats
data[name]:
data[substance]:
data[atc_code]:
data[year]: n/a
data[gender]: n/a
data[age_group[]][]: 3
data[county]: n/a

POST data, encoded

action=search&entity=DrugStats&data%5Bname%5D=&data%5Bsubstance%5D=&data%5Batc_code%5D=&data%5Byear%5D=n%2Fa&data%5Bgender%5D=n%2Fa&data%5Bage_group%5B%5D%5D%5B%5D=3&data%5Bcounty%5D=n%2Fa

Processing code

$.ajax({
        url: 'ajax.php',
        type: 'post',
        dataType: 'json',
        data: {
               'action': 'search',
               'entity':  $(entity).data('entity'),
               'data':    data,
               'page':    page
              },
        success: function(data) {
            // code
        },
        complete: function() {
            // code
        }
      });
silkfire
  • 24,585
  • 15
  • 82
  • 105
  • Are you sure with why they are thr? – swapnesh Sep 11 '13 at 08:33
  • can you show us the POST http header? Is your doctype and encoding correct (both in header and file)? also does that happen in every browser? – x4rf41 Sep 11 '13 at 08:37
  • Looks like no problem codes – Bora Sep 11 '13 at 08:37
  • @x4rf41 Like that? See my edit. – silkfire Sep 11 '13 at 08:39
  • i think the problem lies in the processing code, can you show your processing code? – Liam Allan Sep 11 '13 at 08:40
  • does is work with a normal form submit (without ajax) ? and can you show the body of the request (that is where the POST variables are)? if it works with a normal submit, then its a javascript problem and you should show your javascript, if it doesnt its probably an encoding problem, but thats very unlikely – x4rf41 Sep 11 '13 at 08:43
  • Are you sanitizing the data? Have you tried jQuery.serialize() ? That should in theory help. – DarkMantis Sep 11 '13 at 08:45
  • I mean it always worked until I added that field which requires a multi select =/ – silkfire Sep 11 '13 at 08:47
  • I'm reading this thread http://stackoverflow.com/questions/6011284/problem-with-brackets-in-jquery-form-data-when-sending-data-as-json and it says that my PHP server should able to decode the bracket syntax properly. But it fails to do so? – silkfire Sep 11 '13 at 08:51
  • @x4rf41 Added the POST data as well. – silkfire Sep 11 '13 at 08:54
  • the way jquery serializes the select make thes brakets unneccesary and i think this: `data[age_group[]]` is confusing php, try to name the select `name="age_group"` without brackets. i think it should work (even for multiselects) – x4rf41 Sep 11 '13 at 09:26
  • @x4rf41 It does work if I rename to just `age_group`, but I need `[]` because I'm submitting the form when performing my Excel export, and without them that field will turn into a string instead of an array. I need to work both ways :/ I tried escaping those brackets with backslash and double backslash to no avail. Just confuses PHP... – silkfire Sep 11 '13 at 09:28
  • Was this (incredibly old) thread ever solved? I'm having the same issue. – Erin Jul 28 '16 at 12:18
  • @Erin Not sure if I ever solved it or if I decided to implement a workaround instead :) – silkfire Jul 28 '16 at 13:15

0 Answers0