2

I tried to submit my form using ajax so i wrote this script :

$(document).ready(  
function doAjaxPost() {
        var email = $('#user_email').val();
        var firstName = $('#user_firstName').val();
        var lastName = $('#user_lastName').val();
        var password = $('#user_password').val();

        $
                .ajax({

                    type : "POST",
                    async: false ,
                    url : "home1",
                    data : "email=" + email + "&firstName=" + firstName
                            + "&lastName=" + lastName + "&password=" + password,

                    success : function(response) {

                        alert('Error: '+ "test");
                        if(response.result=="error")
                        $('#info').html(
                                response.message);

                    },

                    error : function(e) {

                        alert('Error: ' + e);

                    }

                });

    }
);
$('#myForm').submit(doAjaxPost());

then i included it in my page.html.

but when i load the page the script executed but the script must intercept submit event.

how can i prevent the script to execute when i load my page ?

4
  • You need to URL-encode your parameters. (or let jQuery do that for you). Also, never use async: false. Commented Aug 14, 2014 at 15:32
  • Why i need URL-encode ? Commented Aug 14, 2014 at 22:47
  • What if one of the values has an &? Commented Aug 15, 2014 at 2:00
  • You're right.I will considerate your remark despite it's not my problem right now. Commented Aug 15, 2014 at 15:44

3 Answers 3

2

I think I've found a solution by moving my function code into submit scope :

$(document).ready(
$('#myForm').submit(function() {
    var email = $('#user_email').val();
    var firstName = $('#user_firstName').val();
    var lastName = $('#user_lastName').val();
    var password = $('#user_password').val();

    $
            .ajax({

                type : "POST",
                async: false ,
                url : "home1",
                data : "email=" + email + "&firstName=" + firstName
                        + "&lastName=" + lastName + "&password=" + password,

                success : function(response) {

                    alert('Error: '+ "test");
                    if(response.result=="error")
                    $('#info').html(
                            response.message);

                },

                error : function(e) {

                    alert('Error: ' + e);

                }

            });

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

Comments

0

I think your problem is the last line of code:

$('#myForm').submit(doAjaxPost());

whit this code you are saying to jquery to fire the "submit" event to the form. Try to replace it with:

$('#myForm').bind('submit', doAjaxPost);

1 Comment

thanks Mattia, I've tried but it doesn't work for me.
0

Just swap the last two lines and take out async: false.

1 Comment

Thank you for the reply, but i didn't understand what do you mean by submit the form inside the ajax callback (sorry but i'm new with ajax ).

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.