0

I want to submit my form using ajax.

Below is my code:

$("#loginForm").submit(function() {
var url = <url>;
$.ajax({
       type: "POST",
       url: url,
       data: $("#loginForm").serialize(), // serializes the form's elements.
       success: function(data)
       {
            if(data == '0'){ 
                    $("#loginErr").show();
                    return false;
            }
            else if(data == '1')
                    return true;
       }
     });
  return false;
});

I want to get my form submitted if ajax response is 1. but irrespective of data value, form is not getting submitted. Its always getting return value false. I have checked it reaches to else if condition, but execution is not stopped on return.

How can I submit my form? I want to refresh my page.

8
  • 5
    AJAX is async btw you cannot return value from success callback function Commented Jun 27, 2013 at 12:38
  • Your url variable is an actual URL in your real code, correct? Commented Jun 27, 2013 at 12:38
  • please post your server side code... Commented Jun 27, 2013 at 12:39
  • @roasted, what can I do then? Commented Jun 27, 2013 at 12:39
  • @Aijaz, I have checked, it reaches to else if condition, but execution is not stopped. Commented Jun 27, 2013 at 12:41

3 Answers 3

1

Use that:

$("#loginForm").submit(function (event) {
    var url = < url > ;
    $.ajax({
        type: "POST",
        url: url,
        context:this, //setting context on form
        data: $("#loginForm").serialize(), // serializes the form's elements.
        success: function (data) {
            if (data == '0') $("#loginErr").show();    
            else if (data == '1') this.submit();//use js submit event, not jq. We need to manually submit it now that we know its successful
        }
    });
    event.preventDefault(); //Stop the form submitting straight away
});
Sign up to request clarification or add additional context in comments.

6 Comments

check if condition if (data == '1') is meet
what give you: console.log(this) inside this condition?
It says that its a form object
And removing this submit handler $("#loginForm").submit(...), is your form submitted? What if you try $(this).off('submit').submit()?
Pls ignore my previous comment, It gives: Object { url=<url>, type="POST", more...}
|
0

I am not sure which data you would like checked. But if this data comes from the form itself, then you need to check it before ajax call. Once you make the ajax post call, your form is already submitted.

 $("#loginForm").submit(function(e) 
 {

 var url = <url>;
 e.preventDefault();
 mydata = $("#loginForm").serialize();
 //Your check here
 if(mydata == '0')
 { 
      $("#loginErr").show();
      return false;
 }

 $.ajax({
   type: "POST",
   url: url,
   data: mydata,
   success: function(response)
   {
        //this is your post response processor
   }
 });
 return false;
 });

Comments

0

As I wanted to refresh my page, I did want synchronous request. Setting async false solved the issue. I am able to get ret value outside Ajax object as it is requested synchronously.

$("#loginForm").submit(function() {
var url = <url>;
var ret = false;
$.ajax({
       async: false,
       type: "POST",
       url: url,
       data: $("#loginForm").serialize(), // serializes the form's elements.
       success: function(data)
       {
           //alert(data); // show response from the php script.
            if(data == '1')
                    ret = true;
            else if(data == '0')
                    $("#loginErr").show();
       }
     });
     return ret;
});

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.