金狮镖局 Design By www.egabc.com
本文实例讲述了原生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文件
<"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"; } "htmlcode"><!-- 保存cookie --> <script> // saveCookie //保存cookie //参数: //键 //值 //有效期(单位是:天) //返回值:无 function saveCookie(key,value,dayCount){ var d = new Date(); d.setDate(d.getDate()+dayCount); document.cookie = key+'='+escape(value)+';expires='+d.toGMTString(); } //获取cookie(根据键获取值) //参数: //键 //返回值:值 function getCookie(key){ var str = unescape(document.cookie);//username=jzm; userpass=1236667 //1、分割成数组(; ) var arr = str.split('; ');//['username=jzm','userpass=1236667'] //2、从数组查找key=; for(var i in arr){ if(arr[i].indexOf(key+'=')==0){ return arr[i].split('=')[1]; } } return null; } //删除cookie //参数: //键 //返回值:无 function removeCookie(key){ saveCookie(key,'',-1); } </script>更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中ajax操作技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无原生javascript的ajax请求及后台PHP响应操作示例的评论...
更新日志
2024年09月23日
2024年09月23日
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]
- 洪荣宏1981-歹路不可行·相思雨(飞跃复刻版)[光美][WAV+CUE]
- 旅行团2015-10Day’s[来福胶泥][WAV+CUE]
- 龙千玉.2011-卡将哟【豪记】【WAV+CUE】
- 蔣志光.1993-多情多寂寞(国专)【BMG】【WAV+CUE】
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[FLAC/分轨][251.78MB]
- 黄韵玲2001-巴结音乐[全员集合][WAV+CUE]
- 郑绪岚2015-流金岁月·精选[首版][WAV+CUE]
- 周冰倩1995《沧桑情歌》JVC日本录制[WAV+CUE]
- 辛禄乐队.2024-热烈的少年【星辰】【FLAC分轨】
- 成龙.1996-龙的心【滚石】【WAV+CUE】