国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

原生javascript的ajax請求及后臺PHP響應操作示例

2024-05-06 15:44:49
字體:
來源:轉載
供稿:網友

本文實例講述了原生javascript的ajax請求及后臺PHP響應操作。分享給大家供大家參考,具體如下:

<!doctype html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title></head><body>  <table id="t">    <tr>      <td>學號:</td>      <td><input type="text" id="stuid" /></td>    </tr>    <tr>      <td>密碼:</td>      <td><input type="password" id="stupass" /></td>    </tr>    <tr>      <td colspan="2">        <input id="btnLogin" type="button" value="登錄" />      </td>    </tr>  </table></body></html>

ajax的一般步驟

1、創建對象

let xhr = new XMLHttpRequest();

2、設置請求參數

xhr.open(請求方式,請求地址,是否異步);

3、設置回調函數

xhr.onreadystatechange = function () {    // 5、接收響應  alert(xhr.responseText);}

4、發送

xhr.send();
<script type="text/javascript">window.onload = function(){  $("#btnLogin").onclick = function(){    //1、創建對象    let xhr = new XMLHttpRequest();    //2、設置請求參數    xhr.open('post','loginCheckajax.php',true);    //3、設置回調函數    xhr.onreadystatechange = function(){      if(xhr.readyState==4 && xhr.status==200){        if(xhr.responseText=='1'){          //存cookie          saveCookie("username",$("#stuid").value,7);          //挑到首頁          location.href="index.html" rel="external nofollow" ;        }else{          alert("登錄失敗!");        }      }    }    xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');    //4、發送    xhr.send("stuid="+$("#stuid").value+"&stupass="+$("#stupass").value);  }}function $(str){  //id class tagname  if(str.charAt(0) == "#"){    return document.getElementById(str.substring(1));  }else if(str.charAt(0) == "."){    return document.getElementsByClassName(str.substring(1));  }else{    return document.getElementsByTagName(str);  }}</script>

php文件

<?php  header("Content-type:text/html;charset=utf-8");  //一、獲取用戶的輸入  $stuid = $_POST['stuid'];  $stupass = $_POST['stupass'];  //二、處理  //1、建立連接(搭橋)  $conn = mysql_connect('localhost','root','root');  if(!$conn){    die("連接失敗");  }  //2、選擇數據庫(選擇目的地)  mysql_select_db("mydb1809",$conn);  //3、執行SQL語句(傳輸數據)  $sqlstr="select * from student where stuid='$stuid' and stupass='$stupass'";  $result = mysql_query($sqlstr,$conn);//結果是個表格  //4、關閉數據庫(過河拆橋)  mysql_close($conn);  //三、響應  if(mysql_num_rows($result)>0){    echo "1";  }else{    echo "0";  }?>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 梁山县| 鹤庆县| 宁远县| 安岳县| 长子县| 堆龙德庆县| 潮安县| 邓州市| 安多县| 林口县| 乐安县| 邵阳县| 江川县| 耒阳市| 铜山县| 普兰店市| 长宁县| 雅江县| 武穴市| 吉林市| 柳江县| 台中市| 德阳市| 呼伦贝尔市| 宜丰县| 徐汇区| 太湖县| 新密市| 阆中市| 定州市| 时尚| 凤山市| 苍溪县| 慈溪市| 台东县| 深圳市| 西平县| 黎川县| 正蓝旗| 东阳市| 万安县|