前台代码
点击(此处)折叠或打开
- <!DOCTYPE html >
- <html>
- <head>
- <title>task</title>
- </head>
- <body>
- <p><label>用户名</label><input type="text" id="username"/></p>
- <p><label>密码</label><input type="password" id="password"/></p>
- <p><input type="submit" value="submit" onclick="doRequestUsingPOST()"/></p>
- </body>
- </html>
- <script language="javascript" type="text/javascript">
- //创建一个XMLHttpRequest对象
- var xmlHttp;
- function createXMLHttpRequest(){
- if(window.ActiveXObject)
- {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- else if(window.XMLHttpRequest)
- {
- xmlHttp = new XMLHttpRequest();
- }else{
- alert("浏览器不支持XMLHttpRequest对象");
- }
- }
- //拼接请求参数
- function createQueryString(){
- var username = document.getElementById("username").value;
- var password = document.getElementById("password").value;
- var queryString = "username="+username+"&password="+password;
- return queryString;
- }
- //开始一个请求
- function doRequestUsingPOST(){
- createXMLHttpRequest();
- var url = "dologin.php";
- var queryString = createQueryString();
- xmlHttp.open("POST",url,true);
- xmlHttp.onreadystatechange = handleStateChange;
- xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
- xmlHttp.send(queryString);
- }
- function handleStateChange(){
- if(xmlHttp.readyState == 4){
- if(xmlHttp.status == 200){
- console.log(xmlHttp.responseText);
- if(xmlHttp.responseText == "ok"){
- alert("ok");
- }else{
- alert("error");
- }
- }
- }
- }
- </script>
点击(此处)折叠或打开
- <?php
- session_start();
- if($_POST['username']&&$_POST['password']){
- $name = $_POST['username'];
- $pass = $_POST['password'];
- if($name=='ella' && $pass = 'pass'){
-
- echo "ok";
-
- }else{
- echo "error";
- }
- }else{
- echo 'error';
- exit;
- }
- ?>