0

I have a problem with Jquery Ajax posting.
Previously I was using

<form method="post" action="manageUserItem.php">....</form> 

to post my input value and insert into database.
But when I try to change to jquery ajax function with this code:

$.validator.setDefaults({
        submitHandler: function() {
            var formData = new FormData($(this)[0]);    
                $.ajax({
                    url:'manageUserItem.php',
                    type: 'POST',
                    data: formData,
                    async: false,
                    beforeSend: function(){
                        if(confirm('Are you sure?'))
                            return true;
                        else
                            return false;
                    },
                    cache: false,
                    contentType: false,
                    processData: false
                }).done(function () {
                        //do something if you want when the post is successfully
                        if(!alert('New User Created.')){document.getelementbyclassname('form').reset()}
                }).fail(function () {
                        //if the post is failed show an error message if you want
                        alert('Some error occur. Please try again later.');
                });
                return false;
        }
    });

then my input field for name, userid and password become empty when insert into the database.
Is there anything wrong in my code?

Please give me some advice, because I'm still new to this.

3
  • Try new FormData(this[0]); maybe. Commented Mar 31, 2015 at 9:11
  • Nope, not working either Commented Mar 31, 2015 at 9:15
  • How about new FormData($('#yourForm'));? Commented Mar 31, 2015 at 9:16

3 Answers 3

2

You can try this:

var formData = new FormData(document.forms[0]);
Sign up to request clarification or add additional context in comments.

Comments

1

You have a problem here :

var formData = new FormData($(this)[0]);    

In this context "this" - is submitHandler

Try use class or id of form like

   var formData = new FormData($(".myFormClass")[0]);    

Comments

0

Try to serialize the data:

var formdata = $( "form" ).serialize();
console.log(formdata);

And to a console.log()

Output would look something like:

name=john_doe&user_id=1&password=my_passowrd

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.