《PHP編程:AJAX的使用方法詳解》要點(diǎn):
本文介紹了PHP編程:AJAX的使用方法詳解,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP編程AJAX作為異步傳輸,局部刷新非常方便,用處很廣!
PHP編程首先,對(duì)于AJAX的使用有4步:
PHP編程1.創(chuàng)建AJAX對(duì)象
PHP編程var xmlHttp = new XMLHttpRequest();
PHP編程2.建立連接 (‘提交方式',‘Url地址')
PHP編程xmlHttp.open('get','./AJAX_XML.xml');
PHP編程3.判斷ajax準(zhǔn)備狀態(tài)及狀態(tài)碼
PHP編程
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState==4 && xmlHttp.status==200) {
}
}
PHP編程4.發(fā)送請(qǐng)求
PHP編程xmlHttp.send(null);??? //get方式參數(shù)為null,post方式,參數(shù)為提交的參數(shù)
PHP編程以下以異步提交用戶名(輸入用戶名之后,異步提交后臺(tái)判斷,前臺(tái)立馬提示是否已注冊,不用提交時(shí)再判斷!)
PHP編程GET方式提交
PHP編程xx.html
PHP編程
<script type="text/javascript">
window.onload=function(){
document.getElementById('username').onblur=function(){
var name=document.getElementById('username').value;
var req=new XMLHttpRequest();
req.open('get','4-demo.php?name='+name);
req.onreadystatechange=function(){
if(req.readyState==4 && req.status==200){
alert(req.responseText);
}
}
req.send(null); //如果send()方法中沒有數(shù)據(jù),要寫null
}
}
</script>
PHP編程用戶名:? <input type="text" name="" id="username">
PHP編程xx.php
PHP編程
<?php
print_r($_GET);
?>
PHP編程1、?? IE不支持中文
PHP編程2、?? =、&與請(qǐng)求的字符串的關(guān)鍵字相混淆.
PHP編程POST提交
PHP編程xx.html
PHP編程
<script type="text/javascript">
window.onload=function(){
document.getElementById('username').onblur=function(){
var name=document.getElementById('username').value;
name=encodeURIComponent(name);
var req=new XMLHttpRequest();
req.open('post','5-demo.php?age='+20);
req.onreadystatechange=function(){
if(req.readyState==4 && req.status==200){
alert(req.responseText);
}
}
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
req.send('name='+name);
}
}
</script>
PHP編程用戶名: <input type="text" name="" id="username">
PHP編程xx.php
PHP編程
<?php
print_r($_POST);
print_r($_GET);
?>
PHP編程1、通過send()發(fā)送數(shù)據(jù)
PHP編程2、必須設(shè)置setRequestHeader()將傳遞的參數(shù)轉(zhuǎn)成XML格式
PHP編程3、post提交可以直接提交中文,不需要轉(zhuǎn)碼
PHP編程4、post請(qǐng)求中的字符也會(huì)和URL中的&、=字符相混淆,所以建議也要使用encodeURIComponent()編碼
PHP編程5、在POST提交的同時(shí),可以進(jìn)行GET提交
PHP編程解決 IE不支持中文?? =、&與請(qǐng)求的字符串的關(guān)鍵字相混淆 問題
PHP編程在js中通過encodeURIComponent()進(jìn)行編碼即可.
PHP編程
window.onload=function(){
document.getElementById('username').onblur=function(){
var name=document.getElementById('username').value;
name=encodeURIComponent(name); //編碼
var req=new XMLHttpRequest();
req.open('get','4-demo.php?name='+name);
req.onreadystatechange=function(){
if(req.readyState==4 && req.status==200){
alert(req.responseText);
}
}
req.send(null); //如果send()方法中沒有數(shù)據(jù),要寫null
}
}
PHP編程1、req.responseText:獲取返回的字符串
PHP編程2、req.responseXML:按DOM結(jié)構(gòu)獲取返回的數(shù)據(jù)
PHP編程注意post/get兩種提交方式的區(qū)別!
PHP編程以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持維易PHP!
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/884.html