How to return the value when ajax is triggered using the onsubmit event?

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

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.