Php+ajax implementation of User login verification problem solving?

Source: Internet
Author: User
form in the user name and password two properties, click the button trigger Ajax function, this AJAX function extracts the form "User name" and "password" value sent to the background program verification, PHP writing background program, PHP program for "User name" and "password" to verify, user name error to give a hint, Back to Ajax, display in, the same password error, displayed in, now the user name and password can be correctly uploaded to the PHP program, to make the correct verification, but Xmlhttp.responsetext, how to achieve the above effect? Urgent!!!


Reply to discussion (solution)

Post your code!

You need to assign a conditional value to a different target in the callback function, then return the control information.

//JavaScript documentvar xmlhttp;function s_xmlhttprequest () {if (window. Activxobject) {xmlhttp=new activexobject (' mcrosoft.xmlhttp ');} else if (window. XMLHttpRequest) {xmlhttp=new XMLHttpRequest;}}   function php100 () {s_xmlhttprequest (); user=document.myform.user.value;pwd=document.myform.pwd.value; var poststr = ' user= ' + user+ ' &pwd= ' +pwd;xmlhttp.open (' POST ', ' login3.php ', true); Defines the transmitted file HTTP header information Xmlhttp.onreadystatechange=byphp;xmlhttp.setrequestheader (' Content-type ', ' application/ X-www-form-urlencoded '); Send post Data xmlhttp.send (POSTSTR);} function byphp () {var Byphp100=xmlhttp.responsetext;document.getelementbyid (' UserInfo '). innerhtml=byphp100;} JavaScript document//JavaScript Document 
Form Name= "MyForm" method= "POST" action= "" > User name:   
Password
 
  

The information returned by the user name validation is displayed in the UserInfo Div, and the password is displayed in the Passinfo div; How should the code be changed? Hope to be more detailed, novice Thank you!

You don't have a callback.

var Byphp100=xmlhttp.responsetext;
document.getElementById (' UserInfo '). innerhtml=byphp100; Is this not a callback process?

function byphp () {  var byphp100=xmlhttp.responsetext;  if (/user/.test (byphp100))    document.getElementById (' UserInfo '). innerhtml=byphp100;  else    document.getElementById (' Passinfo '). innerhtml=byphp100;}

Index.html





Untitled Document




User name:


Password







Ajax.js
var http_request;
function Send_request (Url,method) {
Http_request = false;
if (window. XMLHttpRequest) {
Http_request = new XMLHttpRequest ();
if (Http_request.overridemimetype) {
Http_request.overridemimetype (' Text/xml ');
}
}
else if (window. ActiveXObject) {
try {
Http_request = new ActiveXObject ("Msxml2.xmlhttp");
} catch (e) {
try {
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}
Switch (method) {
Case 1:http_request.onreadystatechange = Chk;break;
}
Http_request.open ("GET", url, True);
Http_request.send (NULL);
}

function Check () {
Send_request ("check.php?action=" +document.getelementbyid (' pw '). value,1);


function Chk () {
if (http_request.readystate = = 4) {
if (Http_request.status = = 200) {
document.getElementById ("User"). innerhtml= "";
document.getElementById ("User"). Value=http_request.responsetext;
} else {
Alert ("The page exception that you requested. ");
}
}else {
document.getElementById ("User"). innerhtml= "reading data ...";
}
}


check.php


if (exists) {
echo "Right";
}else{
Echo is "not correct";
}
?>

check.php $_post[' username ' and $_post[' PW ']

function byphp () {  var byphp100=xmlhttp.responsetext;  if (/user/.test (byphp100))    document.getElementById (' UserInfo '). innerhtml=byphp100;  else    document.getElementById (' Passinfo '). innerhtml=byphp100;}

Code to run, I still can not display the "User name" and "password" verification return information into different div? How does the callback control information be written? Thank you! --Novice!

Pass the JSON.

Remembered! Isn't it better to use jquery?

  • 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.