原生javascript的ajax請(qǐng)求及后臺(tái)PHP響應(yīng)操作示例
本文實(shí)例講述了原生javascript的ajax請(qǐng)求及后臺(tái)PHP響應(yīng)操作。分享給大家供大家參考,具體如下:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table id="t"> <tr> <td>學(xué)號(hào):</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、創(chuàng)建對(duì)象
let xhr = new XMLHttpRequest();
2、設(shè)置請(qǐng)求參數(shù)
xhr.open(請(qǐng)求方式,請(qǐng)求地址,是否異步);
3、設(shè)置回調(diào)函數(shù)
xhr.onreadystatechange = function () { // 5、接收響應(yīng) alert(xhr.responseText); }
4、發(fā)送
xhr.send();
<script type="text/javascript"> window.onload = function(){ $("#btnLogin").onclick = function(){ //1、創(chuàng)建對(duì)象 let xhr = new XMLHttpRequest(); //2、設(shè)置請(qǐng)求參數(shù) xhr.open('post','loginCheckajax.php',true); //3、設(shè)置回調(diào)函數(shù) xhr.onreadystatechange = function(){ if(xhr.readyState==4 && xhr.status==200){ if(xhr.responseText=='1'){ //存cookie saveCookie("username",$("#stuid").value,7); //挑到首頁(yè) location.href="index.html" rel="external nofollow" ; }else{ alert("登錄失??!"); } } } xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded'); //4、發(fā)送 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、選擇數(shù)據(jù)庫(kù)(選擇目的地) mysql_select_db("mydb1809",$conn); //3、執(zhí)行SQL語(yǔ)句(傳輸數(shù)據(jù)) $sqlstr="select * from student where stuid='$stuid' and stupass='$stupass'"; $result = mysql_query($sqlstr,$conn);//結(jié)果是個(gè)表格 //4、關(guān)閉數(shù)據(jù)庫(kù)(過河拆橋) mysql_close($conn); //三、響應(yīng) if(mysql_num_rows($result)>0){ echo "1"; }else{ echo "0"; } ?>
<!-- 保存cookie --> <script> // saveCookie //保存cookie //參數(shù): //鍵 //值 //有效期(單位是:天) //返回值:無 function saveCookie(key,value,dayCount){ var d = new Date(); d.setDate(d.getDate()+dayCount); document.cookie = key+'='+escape(value)+';expires='+d.toGMTString(); } //獲取cookie(根據(jù)鍵獲取值) //參數(shù): //鍵 //返回值:值 function getCookie(key){ var str = unescape(document.cookie);//username=jzm; userpass=1236667 //1、分割成數(shù)組(; ) var arr = str.split('; ');//['username=jzm','userpass=1236667'] //2、從數(shù)組查找key=; for(var i in arr){ if(arr[i].indexOf(key+'=')==0){ return arr[i].split('=')[1]; } } return null; } //刪除cookie //參數(shù): //鍵 //返回值:無 function removeCookie(key){ saveCookie(key,'',-1); } </script>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript中ajax操作技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- PHP獲取真實(shí)IP及IP模擬方法解析
- php判斷IP地址是否在多個(gè)IP段內(nèi)
- php實(shí)現(xiàn)統(tǒng)計(jì)IP數(shù)及在線人數(shù)的示例代碼
- bt寶塔面板php7.3、php7.4不支持ZipArchive解決方法
- 通過PHP實(shí)現(xiàn)獲取訪問用戶IP
- PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼
- php利用ZipArchive類操作文件的實(shí)例
- PHP生成zip壓縮包的常用方法示例
- php解壓縮zip和rar壓縮包文件的方法
- PHP基于ip2long實(shí)現(xiàn)IP轉(zhuǎn)換整形
相關(guān)文章
JavaScript控制各種瀏覽器全屏模式的方法、屬性和事件介紹
瀏覽器全屏模式的啟動(dòng)函數(shù)requestFullscreen仍然需要附帶各瀏覽器的js方言前綴,相信下面這段代碼需要你花大量的搜索才能湊齊:2014-04-04js刪除對(duì)象中的某一個(gè)字段的方法實(shí)現(xiàn)
這篇文章主要介紹了js刪除對(duì)象中的某一個(gè)字段的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于JS+HTML實(shí)現(xiàn)彈窗提示是否確認(rèn)提交功能
這篇文章主要介紹了基于JS+HTML實(shí)現(xiàn)彈窗提示是否確認(rèn)提交功能,需要的朋友可以參考下2020-06-06js實(shí)現(xiàn)鼠標(biāo)觸發(fā)圖片抖動(dòng)效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)觸發(fā)圖片抖動(dòng)效果的方法,通過定時(shí)器定時(shí)遞歸調(diào)用rattleimage函數(shù)實(shí)現(xiàn)抖動(dòng)效果,非常實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02php main 與 iframe 相互通訊類(js+php同域/跨域)
這篇文章主要介紹了php main 與 iframe 相互通訊類(js+php同域/跨域),需要的朋友可以參考下2017-09-09