PDA

View Full Version : how to make submit event run after execution my function



smansakra
11-30-2008, 05:48 AM
hello....
can anyone here tell me, HOW to make "submit event" on form run after it executes my function?

please see the example below...

<form name="myform" action="login.php" method="post" onsubmit="checking_valid_then_checking_ajax()">
<input type="text" name="u">
<imput type="password" name="p">
<input type="submit" value="submit">
</form>


then the function is below..

function checking_valid_then_checking_ajax(){
if(document.myform.u=="" || document.myform.p==""){
alert('please complete form!');
return false;
}
cheking_ajax();
}

base on the function above, if the user doesnt enter his username and apssword during submitting the form, of course "submiting activity by the form" will be stop.

BUT, if all ok, function will continue to excute "checking_ajax()".

HERE is my problem,

before my code finish its job to execute "checking_ajax()", the "submit event" is still running... so before my checking_ajax() executed, the form has redirected the page to login.php

WHAT i want is, stop "submit event" first, then finish cheking_ajax(), and after "cheking_ajax" return the value "OK", run "submit event" again.

anyone?

jscheuer1
11-30-2008, 08:04 AM
You will have no control over the submit action unless the onsubmit event returns true or false. So, to begin with you should do this:


onsubmit="return checking_valid_then_checking_ajax();"

Now, let's look at what happens in your function. If u or p are empty, it returns false. Otherwise it hands over to cheking_ajax() and returns undefined. I can't see what cheking_ajax() does. It should be setup to return true or false, ex:


function cheking_ajax(){
if (something)
return true;
alert('no ajax');
return false;
};

or:


function cheking_ajax(){
if (something){
alert('no ajax');
return false;
}
return true;
};

If it is like one of those (the alerts are optional), then your checking_valid_then_checking_ajax() function can be:


function checking_valid_then_checking_ajax(){
if(document.myform.u=="" || document.myform.p==""){
alert('please complete form!');
return false;
}
return cheking_ajax();
}

and it should all work out. But your cheking_ajax() may hand off to yet another function, if it does, then it must return a value of true or false as derived from that function.

smansakra
11-30-2008, 10:12 AM
HI, yes....i got it, i totally understand what you wrote!,

thanks very much, i never think about "onsubmit="return checking_valid_then_checking_ajax()"

:cool: NICE TO MEET YOU