How can I return the returned value when I use the onsubmit event to trigger ajax? this is a form & lt ;! DOCTYPE & nbsp; html & nbsp; PUBLIC & nbsp; "-// W3C // DTD & nbsp; XHTML & nbsp; 1.0 & nbsp; transitional/how to return the value when I use the onsubmit event to trigger ajax?
This is a form.
Untitled Document
Script
Script
This is the xmlhttprequest. js page.
Var xmlhttp = false;
If (window. XMLHttpRequest) {// Mozilla, Safari, and other browsers
Xmlhttp = new XMLHttpRequest ();
}
Else if (window. ActiveXObject) {// IE browser
Try {
Xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
The js2.js page shows the entire execution process with the routes marked by me.
Var flag;
Function check_form (){
Var username = document. getElementById ('Username'). value;
Var identifying_code = document. getElementById ('Identifying _ code'). value;
Url = 'login _ chk. php? Username = '+ username +' & identifying_code = '+ identifying_code;
Alert ('Here step 2 ');
Xmlhttp. open ('GET', url, true );
Alert ('Here step 2 ');
Xmlhttp. onreadystatechange = function (){
Alert ('Here step 2 ');
If (xmlhttp. readyState = 4 ){
Alert ('Here step 2 ');
If (xmlhttp. status = 200 ){
Alert ('Here step 2 ');
Msg = xmlhttp. responseText;
Alert ('Here step 2 ');
If (msg = 1 ){
Document. getElementById ("tishi"). innerHTML = "The input is correct and the jump is in progress ";
Flag = true;
}
Else {
Document. getElementById ("tishi"). innerHTML = "incorrect user name or password ";
Flag = false;
}
Alert ('Here step 2 ');
}
}
// Alert (flag );
}
Xmlhttp. send (null );
Alert ('Here step 2 ');
If (flag = true ){
Alert ('Here is true ');
Alert (flag );
Return true;
}
Else {
Alert ('Here step 2 ');
Alert (flag );
Return false;
}
}
This is the login_chk.php page.
if(strcmp($_GET['username'],'abcd')==0&&strcmp($_GET['identifying_code'],'abcd')==0){
$msg=1;
}
else{
$msg=0;
}
echo $msg;
?>
The most important thing is to obtain the check_form return value to determine whether the form can be submitted. Ask the experts to change the code and get the check_form return value.
------ Solution --------------------
Xmlhttp. open ('GET', url, true );
This is asynchronous communication, so you need to define a callback function that accepts the returned data.
Xmlhttp. onreadystatechange = function (){