原生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);
//挑到首頁
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語句(傳輸數(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事件學(xué)習(xí)小結(jié)(二)js事件處理程序
這篇文章主要介紹了JavaScript事件學(xué)習(xí)小結(jié)(二)js事件處理程序的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
javascript動(dòng)態(tài)創(chuàng)建鏈接的方法
這篇文章主要介紹了javascript動(dòng)態(tài)創(chuàng)建鏈接的方法,涉及javascript動(dòng)態(tài)操作頁面元素的技巧,需要的朋友可以參考下2015-05-05
bootstrap表格內(nèi)容過長(zhǎng)時(shí)用省略號(hào)表示的解決方法
這篇文章主要介紹了bootstrap表格內(nèi)容過長(zhǎng)時(shí)用省略號(hào)表示的解決方法,需要的朋友可以參考下2017-11-11
微信小程序開發(fā)實(shí)戰(zhàn)快速入門教程
這篇文章主要為大家介紹了開發(fā)一個(gè)微信小程序?qū)崙?zhàn)快速入門教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
使用Javascript在HTML中顯示實(shí)時(shí)時(shí)間
這篇文章主要為大家詳細(xì)介紹了使用Javascript在HTML中顯示實(shí)時(shí)時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
JavaScript編碼風(fēng)格精選指南(編寫可維護(hù)的代碼規(guī)范)
javascript編碼規(guī)范能夠增強(qiáng)代碼的簡(jiǎn)潔性、可讀性、可擴(kuò)展性,項(xiàng)目做到后期,每修改一次,所耗費(fèi)的成本就越高,編碼規(guī)范能節(jié)省這樣的成本,并且能很好拓展升級(jí)原有系統(tǒng)功能,javascript編碼規(guī)范也是開源社區(qū)大家約定俗成的規(guī)則!2024-06-06

