1

It's pretty awkward that I couldn't find a thread with a similar problem... Well then, let's assume I have a standard HTML form with checkboxes (that will be parsed as an array in PHP), notice the brackets in the name attribute:

<form class="myForm" action="evaluate.php" method="get">
    <input type="text" name="name" value="" placeholder="Your name"/>
    <input type="checkbox" name="facts[]" value="1" id="fact-1"/><label for="fact-1">Fact 1</label>
    <input type="checkbox" name="facts[]" value="2" id="fact-2"/><label for="fact-2">Fact 2</label>
    ...
</form>

And now I want jQuery to serialize the content of the form with:

alert($('.myForm').serialize());

The expected result would be something like this:

name=MyName&facts[]=1&facts=[]=2&...

but unfortunately it doesn't, because the brackets "[]" are escaped:

name=MyName&facts%5B%5D=1&facts=%5B%5D=2&...

Has anyone a solution for this problem except writing an own serialization script?

kernel
  • 3,654
  • 3
  • 25
  • 33
  • 1
    OK, I found a thread talking about that here: http://stackoverflow.com/questions/304518/how-to-send-serialize-form-data-using-jquery-if-the-input-element-is-an-array but there is no accepted solution. "serialize().replace('%5B%5D', '[]')" is a little risky I think. – kernel Oct 07 '11 at 18:35
  • please take some time to read the [faq]. If there isn't an answer that suits you, you'll just have to add a bounty to it. I understand that you're new and don't have the rep to assign a bounty, but it's really not all that difficult to answer a few questions and get the 50 rep necessary to open a small bounty. – zzzzBov Oct 07 '11 at 18:39
  • k I'll heed your advice. – kernel Oct 07 '11 at 18:47

1 Answers1

3

or try:

alert(decodeURI('MyName&facts%5B%5D=1&facts=%5B%5D=2&'));

it will decode all escaped characters including []

Vaibhav Gupta
  • 1,592
  • 1
  • 13
  • 23