利用ajax實現簡單的註冊驗證局部重新整理執行個體_AJAX相關

來源:互聯網
上載者:User
1,ajax(asynchronouse javascript and xml)非同步 javascrip 和xml
2,(包含了7種技術:javascript xml xstl dom xhtml css xmlhttpRequest)
3,是一種與伺服器語言無關的技術,可以用在(php/jsp/asp.net)
4,ajax的工作原理: 建立一個ajax引擎->發送資料給伺服器--》通過回呼函數接收資料---》將資料賦給指定的頁面

下面是註冊驗證案例register。php是註冊頁面。registerProcess.php用於接收資料並返回資料

register.php
複製代碼 代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>使用者註冊</title>
<!-- 告訴瀏覽器不要緩衝 -->
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
<!--
function sendRequest(){
//建立一個ajax引擎
var xmlHttpRequest;
//不同瀏覽器擷取xmlRequest對象方法不一樣
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("MMMMMictofrt");
}else{
xmlHttpRequest = new XMLHttpRequest();
}
return xmlHttpRequest;
}
var myXmlHttpRequest
function checkName(){
myXmlHttpRequest = sendRequest();
if(myXmlHttpRequest){
//window.alert("建立成功");
//第一個參數表示請求方式get.post。第二個參數指定url,對哪個頁面發送ajax請求
//用get方式提交的資料如果沒有發生變化的話瀏覽器將會從緩衝中直接提取
var url = "/ajax_yhyz/registerProcess.php? username=getValue('username').value";
//1,用這樣就可以不從緩衝提取了
//var url = "/ajax_yhyz/registerProcess.php? mytime='new Date()' & username=getValue('username').value";
//2,<meta http-equiv="pragma" content="no-cache">告訴瀏覽器不要緩衝
//window.alert(url); 用於測試url是否成功
//第三個參數ture 表示非同步機制
myXmlHttpRequest.open("get",url,true);
//指定回呼函數,chul是函數名,表示如果狀態發生變化就調用該函數,有四個狀態
myXmlHttpRequest.onreadystatechange = chuli;
//發送請求,如果是get請求則填寫null即可
//額如果是post請求,則填寫實際的資料
myXmlHttpRequest.send(null);
}
}
function chuli(){
//window.alert("處理函數被回調");
if(myXmlHttpRequest.readyState == 4){
//window.alert("伺服器返回"+myXmlHttpRequest.responseText);
getValue("myres").value = myXmlHttpRequest.responseText;
}
}
function getValue(id){
//return document.getElementById(id).value; 這樣做的話沒辦法完成局部重新整理
return document.getElementById(id);
}
-->

</script>
</head>

<body>
<form action="???" method="post">
使用者名稱字:<input type="text" name="username1" id="username" ><input type="button" onclick="checkName()" value="驗證使用者名稱">
<input type="text" id="myres" />
<br/>
使用者密碼:<input type="password" name="password"><br>
電子郵件:<input type="text" name="email"><br/>
<input type="submit" value="使用者註冊">
</form>
<form action="???" method="post">
使用者名稱字:<input type="text" name="username2" >

<br/>
使用者密碼:<input type="password" name="password"><br>
電子郵件:<input type="text" name="email"><br/>
<input type="submit" value="使用者註冊">
</form>
</body>
</html>

regiseterProcess.php
複製代碼 代碼如下:

<?php
$username=$_REQUEST['username'];
if($username=="shunping"){
echo "err";
}else{
echo "ok";
}
?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.