7

I've got a problem with the form.serialize() function in jQuery. When I try to submit my serialized form via AJAX, serialize() only returns me an empty string.

Perhaps there's a problem with my HTML outline:

<form id="category-dynamic" class="dynamic">
   <fieldset id="inner-fieldset">
      <legend id="new-category">
        <label for="category-name">Category Name: </label>
        <input type="text" name="category-name" value="" />
      </legend>
      <ul id="category-fields">
         <li>
           <label>Field #1:</label><br />
           <input type="text" name="fields[]" value="" />
         </li>
         <li>
           <label>Field #2:</label><br />
           <input type="text" name="fields[]" value="" />
         </li>
      </ul>
   </fieldset>
</form>

In my jQuery function I simply call:

$.post("processor.php", $('#category-dynamic').serialize(), function(data){
     // data handling here...
});
7
  • Where do you get the empty string ?can you show your php code? Commented May 28, 2011 at 15:35
  • As a test, your code appears to be working fine: jfcoder.com/test/serialize.php All that I do is <?php print_r($_POST); ?> on processor.php. Make sure and have console open. Commented May 28, 2011 at 15:39
  • 1
    serialize() most certainly does not return an empty string: jsfiddle.net/fallen888/HFTV6 Commented May 28, 2011 at 15:42
  • Before your $.post() .. add alert($('#category-dynamic').serialize()) to prove to yourself serialize() is returning data Commented May 28, 2011 at 15:44
  • of course serialize(9 does not return an empty string, but it does, the problem is on the client side ( $('#category-dynamic').serialize() in the java console also returns "" ) so the PHP script is not necessary here Commented May 28, 2011 at 15:45

6 Answers 6

44

A heads up for others who might encounter this problem. Apart from not being disabled, the <input> fields must also have name attribute for serialize() to work.

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

2 Comments

In the code provided, the inputs DO have a "name".
Finally someone put me in the right direction! I wish seen this post an hour ago.
13

I had a similar problem to this. When debugging the JavaScript I could see the input values within the form but when calling serialize() the resulting string was empty.

It turned out I was disabling my input elements on the form before calling serialize(). To fix it I changed the code to retrieve the form values before disabling, then use the form values string in the post method.

// Disabling the input fields breaks serialize, so get the values string first
var formValues = form.serialize();
form.find('input').attr('disabled', 'disabled'); 
// Now post the form to the server
$.post(this.action, formValues, function (data)
{
  //...

Comments

5

Late answer, but relevant.

In addition to input fields requiring names, the FORM itself must also have a name, not just an ID.

Comments

4

In case this helps someone in the future, it will also return an empty string if its a <form> within a <form> (which is already a no-no obviously but something to check).

Comments

0

I got an ASP.NET MVC app Using @Html.TextBoxFor which set a name attribute automatically.

In my case I had 2 forms with the same name for which one of them had no inputs at all. I renamed the one with inputs that was resulting in an empty string and everything was fine when serializing it.

The JQuery was picking the empty form of the same name.

Comments

0

I had a similar problem. I know it happens to all of us.

i was doing $('primary_form').serialize() (missing the selector symbol.)

fixed it with $('#primary_form').serialize()

Comments

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.