现在js中:
function startRequest(){
createXMLHttpRequest();
try{
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("POST","loginController.php",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("username=ttt&password=123");
}catch(exception){
alert("您要访问的资源不存在!");
}
}
html中:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="a.js" charset='utf8'></script>
</head>
<body>
  <table width="200" border="1">
    <tr>
      <td>用户名</td>
      <td><input id="u" type="text" name="username" /></td>
    </tr>
    <tr>
      <td>密码</td>
      <td><input id="p" type="text" name="password" /></td>
    </tr>
    <tr>
      <td><input type="button" value="return ajax responseText's value"
onclick="startRequest();" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</body>
</html>
验证php:
<?php
header("Content-Type=text/html;charset=utf-8;");
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username)){
echo "1用户名不能为空<br>";
echo "<a href=index.php>return</a>";
return;
}

if (empty($password)){
echo "2密码不能为空<br>";
echo "<a href=index.php>return</a>";
return;
}

if ($username == 'ttt' && $password == '123'){
echo "3成功";
return;
}else{
echo "4用户名或密码错误<br>";
echo "<a href=index.php>return</a>";
echo $username;
echo $password;
return;
}
?>现在红字的那部分写死了,没有问题。不过如何改成文本框输入的动态呢

解决方案 »

  1.   

    function startRequest(){
    createXMLHttpRequest();
    try{
    xmlHttp.onreadystatechange=handleStateChange;
    xmlHttp.open("POST","loginController.php",true);
    }catch(exception){
    alert("您要访问的资源不存在!");
    }
    }
    html中:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="a.js" charset='utf8'></script>
    </head>
    <body>
      <form name="form1" onsubmit="startRequest();">
      <table width="200" border="1">
      <tr>
      <td>用户名</td>
      <td><input id="u" type="text" name="username" /></td>
      </tr>
      <tr>
      <td>密码</td>
      <td><input id="p" type="text" name="password" /></td>
      </tr>
      <tr>
      <td><input type="submit" value="return ajax responseText's value"/></td>
      <td>&nbsp;</td>
      </tr>
      </table>
      </form>
    </body>
    </html>
      

  2.   


    var uname = document.all.uname.value;
    var pw = document.all.password.value;
    xmlHttp.send("username="+uname+"&password="+pw);