How return values are returned when using the OnSubmit event to trigger Ajax

Source: Internet
Author: User
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 () {
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.