0

I spent hours today to find the solution of my problem. I have a from which has tag onsubmit="return checking();". the checking() function will do the input validation.

function checking(){
    var filename = $("#filename").val();
    $.ajax({
        type: "POST",
        url: url,
        data: $(".myform").serialize(),
        success: function(data){
            if(data == "something"){
               return true;
            }
            else{
               return false;
            }
        }
    });
return false;     // this will trigger my onSubmit
}

but this one is not working because the return value which is true or false is inside the $.ajax function. So how can I trigger the onsubmit in my form?

Thanks in advance!

leppie
  • 115,091
  • 17
  • 196
  • 297
Bizarre
  • 106
  • 2
  • 12
  • possible duplicate of [Variable doesn't get returned JQuery](http://stackoverflow.com/questions/12475269/variable-doesnt-get-returned-jquery) – Denys Séguret Nov 29 '12 at 08:38
  • @dystroy hey good link, thank you very much. But I really don't know how to implement that one on my codes. when i click submit, then the form will be submitted, even though I put an alert to echo something. this is script in form tag.. onsubmit="return checking(data, callback);" – Bizarre Nov 29 '12 at 09:02

1 Answers1

0

I propose that instead of using a submit button, you use a plain old button like this:

<input id="submit" type="button" value="Submit" />

Then, change your handler so that the submission only occurs if validation passes, instead of trying to stop a submit if validation fails:

function checking(){
    var filename = $("#filename").val();
    $.ajax({
        type: "POST",
        url: url,
        data: $(".myform").serialize(),
        success: function(data){
            if(data == "something"){
               $('#myform')[0].submit(); //submit your form here
            }
            else{
               return false;
            }
        }
    });
    return false;
}
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • thanks Asad, but I am posting a file. and in my form tag, there is action="process.php". That's why I am using the previous way. How to achieve that? and how to use $.submit()? changed the type from submit to button, but where should i put the script to trigger the function checking? – Bizarre Nov 29 '12 at 08:44
  • @Bizarre It doesn't matter inputs are in your form. `formelement.submit` simply does exactly the same thing that clicking a submit button does. – Asad Saeeduddin Nov 29 '12 at 09:25
  • @Bizarre What do you mean form is not submitted? – Asad Saeeduddin Nov 29 '12 at 09:36
  • when i clicked the button, nothing happen. when i put alert before submit the form, it comes out. but the form is not submitted. i am using this one var frm; frm = document.batch; document.batch.action = "process.php"; document.batch.submit(); – Bizarre Nov 29 '12 at 09:39
  • @Bizarre Here is a short example of a form that is submitted if a certain condition is met: http://jsfiddle.net/gp6Zy/1/ – Asad Saeeduddin Nov 29 '12 at 09:51
  • There is nothing happened. Got the same problem, the form is not submitting. Do i need to put the action="process.php" in form tag? I put action script in your link, but nothing happened. it is still in jsfiddle page. – Bizarre Nov 30 '12 at 01:13
  • Hey it works! Thank you so much! works like charm....thumbs up! solved my one day problem after struggling too much. seems really easy. by the way, I am really newbie to ajax and jQuery. good stuff! – Bizarre Nov 30 '12 at 01:22
  • i copied the same codes to the different place, but nothing happen. what is the problem? – Bizarre Nov 30 '12 at 04:14
  • problem solved! to everybody, please use another name for id/class, do not use "submit". my mistakes,cheers! – Bizarre Nov 30 '12 at 06:58