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?

3
  • 1
    OK, I found a thread talking about that here: stackoverflow.com/questions/304518/… but there is no accepted solution. "serialize().replace('%5B%5D', '[]')" is a little risky I think. Commented Oct 7, 2011 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. Commented Oct 7, 2011 at 18:39
  • k I'll heed your advice. Commented Oct 7, 2011 at 18:47

1 Answer 1

3

or try:

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

it will decode all escaped characters including []

Sign up to request clarification or add additional context in comments.

1 Comment

This is one of the handiest js functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.