#1180 new
Ben Gunter

Form.serialize() is broken for multiple selects

Reported by Ben Gunter | December 7th, 2010 @ 05:22 PM

I have a form with a select element with multiple="multiple". I am using Form.serialize(..) to serialize the form to a string and submit it via Ajax.Request. If a multiple select named, say, "foo" has multiple options selected it should serialize to, e.g., foo=1&foo=2&foo=3. After upgrading to Prototype 1.7, it is serializing to foo=1,2,3.

The problem appears to be in Form#serializeElements(..), as it does not account for the possibility that the form element's value is an Array. Form.Element#serialize() does not exhibit this behavior; it serializes the value correctly.

A workaround for this problem is to call $(form).serialize({hash:true}) and pass that to Ajax.Request instead, as Ajax.Request will serialize the resulting hash correctly.

I have attached an HTML page to demonstrate the problem and a patch that I've applied locally to fix it for the time being.

Comments and changes to this ticket

  • yaz

    yaz February 10th, 2011 @ 04:58 PM

    • Tag set to form, multipleselects, serialize

    Had the same problem. Quite a lot of sections on our CMS were broken as a result. The patch is working great.

  • dahearn

    dahearn April 20th, 2011 @ 04:18 PM

    We recently upgraded our Rails app to Prototype 1.7 and use Ajax exclusively to submit forms, and this bug is biting us too. I applied the patch submitted by Ben above (thanks Ben!) and it appears to fix our issue too.

    Is this issue something that is going to be fixed in an upcoming release of Prototype?

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins


Referenced by