How return values are returned when using the OnSubmit event to trigger Ajax
This is a form.
Untitled Document
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) {}
}
}
This is the Js2.js page with my marked route can see the whole process of execution.
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 (' 1th step here ');
Xmlhttp.open (' Get ', url,true);
Alert (' 2nd step here ');
Xmlhttp.onreadystatechange = function () {
Alert (' 5th step here ');
if (xmlhttp.readystate = = 4) {
Alert (' 6th step here ');
if (Xmlhttp.status = = 200) {
Alert (' 7th step here ');
msg = Xmlhttp.responsetext;
Alert (' 8th step here ');
if (msg==1) {
document.getElementById ("Tishi"). Innerhtml= "input correct is jumping";
Flag=true;
}
else{
document.getElementById ("Tishi"). innerhtml= "User name or password error";
Flag=false;
}
Alert (' 9th step here ');
}
}
alert (flag);
}
Xmlhttp.send (NULL);
Alert (' 3rd step here ');
if (flag==true) {
Alert (' Here is true ');
alert (flag);
return true;
}
else {
Alert (' 4th step here ');
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 get the return value of Check_form in order to decide whether the form can be submitted. Ask the master to change the code to take the return value of Check_form
------Solution--------------------
Xmlhttp.open (' Get ', url, true);
This is asynchronous communication, so you have to define the callback function that accepts the return data
Xmlhttp.onreadystatechange = function () {