PHP+Ajax簡單get驗證操作示例
本文實例講述了PHP+Ajax簡單get驗證操作。分享給大家供大家參考,具體如下:
1、顯示頁面代碼
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>XMLHttpRequest獨對象使用實例</title> <script language="JavaScript"> //第一步創(chuàng)建ajax引擎對象 function getXmlHttpObject(){ var XmlHttpRequest; if(window.ActiveXObject){ XmlHttpRequest=new ActiveXObject("Microsoft:XMLHTTP"); } else{ XmlHttpRequest=new XMLHttpRequest(); } return XmlHttpRequest; } var myXmlHttpRequest=""; //處理函數(shù) function process(){ myXmlHttpRequest=getXmlHttpObject(); //判斷xmlHttpRequest是否創(chuàng)建成功 if(myXmlHttpRequest){ //獲取表單的文本框的輸入值 var name=document.getElementById('myname').value; //通過myXmlHttpRequest對象發(fā)送請求到服務(wù)器的某個頁面 var url="process.php?name="+name; myXmlHttpRequest.open("GET",url,true); //定義準(zhǔn)備狀態(tài)改變時的回調(diào)函數(shù) myXmlHttpRequest.onreadystatechange=callBack; //向服務(wù)器發(fā)送請求 myXmlHttpRequest.send(null); } } function callBack(){ if(myXmlHttpRequest.readyState==4){ if(myXmlHttpRequest.status==200){ //解析從服務(wù)器返回的字符串 var result=myXmlHttpRequest.responseText; $("message").innerHTML="<b><u>"+result+"</u></b>" } else{ alert("執(zhí)行過程中出現(xiàn)問題,服務(wù)器返回:"+myXmlHttpRequest.statusText); } } } //通過id獲取元素 function $(id){ return document.getElementById(id); } </script> </head> <body> 請輸入你的名稱 <p> <input type="text" id="myname"> <p> <input type="button" value="提交" onclick="process()"> <div id="message"> </div> </body> </html>
2、處理代碼
proces.php
<?php /** * Created by PhpStorm. * User: yanqiang * Date: 2016/5/25 * Time: 0:40 */ header("Content-Type:text/html;charset=utf-8"); $arr=array("孫悟空","小娜扎","葫蘆娃","米老鼠","唐老鴨","機器貓"); if(in_array($_GET['name'],$arr)){ echo $_GET['name']."存在列表中"; } else{ echo $_GET['name']."不存在列表中"; } ?>
3、效果圖
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php+js實現(xiàn)百度地圖多點標(biāo)注的方法
這篇文章主要介紹了php+js實現(xiàn)百度地圖多點標(biāo)注的方法,涉及php結(jié)合js針對百度地圖接口調(diào)用與json操作相關(guān)技巧,需要的朋友可以參考下2016-11-11php實現(xiàn)word轉(zhuǎn)html的方法
這篇文章主要介紹了php實現(xiàn)word轉(zhuǎn)html的方法,涉及PHP調(diào)用com組件操作word文檔的相關(guān)技巧,需要的朋友可以參考下2016-01-01PHP file_get_contents設(shè)置超時處理方法
今天說的這篇是講超時的,確實在跨服務(wù)器提交的時候,不可避免的會遇到超時的情況,這個時候怎么辦?set_time_limit是沒有用的,只有用context中的timeout時間來控制2013-09-09header中Content-Disposition的作用與使用方法
本文章詳細(xì)的介紹了關(guān)于php header中Content-disposition用法詳細(xì),有需要了解header用法的朋友可參考一下2012-06-06