16.PHP_Ajax模拟服务器登录验证
Ajax模擬登陸驗證
index.php
<script?language="javascript">
????var?http_request?=?false;
????function?createRequest(url){
????????http_request?=?false;
????????if(window.XMLHttpRequest){???????????????????//Mozilla、Safari等瀏覽器
????????????http_request?=?new?XMLHttpRequest();
????????}else?if(window.ActiveXObject){??????????????//IE
????????????try{
????????????????http_request?=?new?ActiveXObject("Msxml2.XMLHTTP");
????????????}catch(e){
????????????????try?{
????????????????????http_request?=?new?ActiveXObject("Microsoft.XMLHTTP");
????????????????}catch(e){}
????????????}
????????}
????????if(!http_request){
????????????alert("不能創建XMLHTTP實例!");
????????????return?false;
????????}
????????http_request.onreadystatechange?=?alertContents;?//指定相應方法
????????//發出HTTP請求
????????http_request.open("GET"?,url?,true);
????????http_request.send(null);
????}
????function??alertContents()?{
????????if?(http_request.readyState?==?4)?{//處理服務器返回的信息
????????????if?(http_request.status?==?200)?{
????????????????alert(http_request.responseText);
????????????}?else?{
????????????????alert("Ajax驗證頁面發生錯誤");
????????????}
????????}
????}
</script>
<script?language="javascript">
????function?checkName(){
????????var?username?=?form1.name.value;
????????if(username?==?""){
????????????window.alert("name?is?null!");
????????????return?false;
????????}else?{
????????????createRequest('checkname.php?username='?+?username?+?'&nocache='?+?new?Date().getTime());
????????}
????}
</script>
<form?name="form1"?method="post"?action="">
????<select?name="name">
????????<option?value="xiaoming">xiaoming</option>
????????<option?value="xiaoli">xiaoli</option>
????????<option?value="xiaowang">xiaowang</option>
????</select>
????<input?type="submit"?name="Submit"?value="Ajax"?οnclick="checkName()">
</form>
?
CheckName.php
<?php
????//模擬服務器驗證
????$username?=?$_GET['username'];
????echo?'Ajax?Check?web?Get?Name:?'.$username;
?>
?
總結
以上是生活随笔為你收集整理的16.PHP_Ajax模拟服务器登录验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 15.PHP_PHP与Ajax
- 下一篇: 开发环境 测试环境 定义_「PHP7数组