日期:2014-05-17  浏览次数:20376 次

急!!PHP+Ajax实现用户登录验证问题求解?
表单里用户名和密码两个属性,单击按钮触发AJAX函数,这个AJAX函数提取表单中“用户名”和“密码”的值发送到后台程序验证,php写的后台程序,php程序分别对“用户名”和“密码”进行验证,用户名出错给出提示,传回给ajax,显示到<div id="user"></div>中,同理密码出错,显示到<div id="pass"></div>中,现在用户名和密码能够正确传到PHP程序中,做出正确验证,但是xmlHttp.responseText,如何能实现上面的效果呀?急!!!

------解决方案--------------------
贴出你的代码!

你需要在回调函数中有条件的对不同的目标赋值,那就要回传有控制信息
------解决方案--------------------
 index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script src="ajax.js"></script>
<body>
<label>
用户名:<input type="text" id="username" name="textfield" />
</label>
<p>
 密码 <input type="text" id="pw" name="textfield2"  onblur="check()"/>
</p>
</body>
</html>
<div id="user"></div>


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("不能创建XMLHttpRequest对象实例.");
  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("您所请求的页面异常。");
    }
   }else {
 document.getElementById("user").innerHTML="正在读取数据中……";
 }
}


check.php

<?php

if(存在){
echo"正确";
}else{
echo "不正确";
}
?>


------解决方案--------------------
传json吧
------解决方案--------------------
留名!   用jquery不是更好吗