1

Having some trouble with serialize. I have my name attributes set but still I'm getting a string with the correct names but no values (even though I'm typing info into the input fields) - something like this:

magnolia_name=&magnolia_email=&magnolia_message=

Driving me crazy. Here's the html code (I commented out the file input, just in case that was the culprit, no luck).

                 <form id="contact" method="POST">
                    <div class="group">      
                      <input type="text" name="magnolia_name" id="magnolia_name" required>
                      <span class="bar"></span>
                      <label>Name</label>
                    </div>

                    <div class="group">      
                      <input type="text" name="magnolia_email" id="magnolia_email" required>
                      <span class="bar"></span>
                      <label>E-mail</label>
                    </div>

                    <div class="group textarea">      
                      <textarea name="magnolia_message" id="magnolia_message"></textarea>
                      <span class="bar"></span>
                      <label>Message</label>
                    </div>

                  <!--   <label for="fileupload" class="fileupload-label">
                        <span>Attach files <span class="glyphicon glyphicon-upload"></span></span>
                    </label>
                    <input id="fileupload" type="file" name="files" multiple /> -->
                    <div class="uploaded-files"></div>
                    <div class="submit">
                        <input type="submit" value="Send!" name="submit" id="submit">
                        <div id="progress">
                            <span>Uploading file(s)</span>
                            <div class="bar"></div>
                        </div>
                    </div>
                </form> 

And here's my JS

var $form = $formContainer.children('form');
var formData = $form.serialize();
$form.submit(function(e){
    console.log(formData);
    e.preventDefault();
});
1
  • you have to serialize the data on submit (inside your method) Commented Aug 28, 2015 at 21:49

3 Answers 3

1

Change your submit handler to capture the formData in the event.

$form.submit(function(e){
    var formData = $form.serialize();
    console.log(formData);
    e.preventDefault();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Ugh - I really need more sleep. Thank you, this fixed it obviously.
1

As mentioned in the comment above. You'll have to serialize the method when you'll submit the form, not before:

$().ready(function() {
    var $form = $('form');

    $form.submit(function(e){
        var formData = $form.serialize();
        console.log(formData);
        e.preventDefault();
    });
});

Example is here

1 Comment

This is correct, but I'd be cautious of assuming a selector … there may be multiple forms on the page.
0
var $form = $('form#contact');
var formData = $form.serialize();
$form.submit(function(e){
    console.log(formData);
    e.preventDefault();
});

I don't understand what $formContainer is as this is not a selector. That is what was breaking your code.

Hope this helps

2 Comments

It's a selector I create further above my code. If that was the issue my serialize wouldn't return the correct names in the string. I still tried; didn't fix it. var $formContainer=$('.about-us > .container > .right');
Most likely the hierarchy is incorrect. Since you have an ID for the form you are better of using that - see jsbin.com/ruqaqi

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.