Ajax異步傳輸與PHP實(shí)現(xiàn)交互示例
更新時(shí)間:2014年01月27日 15:52:47 作者:
Ajax異步傳輸想必大家并不陌生吧,下面為大家介紹下與PHP實(shí)現(xiàn)交互的示例,大家不要錯(cuò)過(guò)
背景
前臺(tái)頁(yè)面兩個(gè)select框,一個(gè)與學(xué)院關(guān)聯(lián),另一個(gè)與專(zhuān)業(yè)關(guān)聯(lián),現(xiàn)需要選擇學(xué)院select框后,顯示學(xué)院相關(guān)信息,且專(zhuān)業(yè)select下面僅有屬于該學(xué)院的專(zhuān)業(yè)名稱。也就是實(shí)現(xiàn)一個(gè)二級(jí)聯(lián)動(dòng)效果。
兩個(gè)select里面分別定義onchange事件,事件中利用ajax的GET方法向后臺(tái)PHP遞交信息,再將查詢得到的信息echo出來(lái)或document.write。
注:代碼參考了有位叫y0umer的博主寫(xiě)的。
<script type="text/javascript">
var XmlHttp;
function createXmlHttpRequestObject(){
if(window.ActiveXobject){ // 判斷是否是ie瀏覽器
try { // try開(kāi)始
xmlhttp = new ActiveXobject("Microsoft.XMLHTTP"); // 使用ActiveX對(duì)象創(chuàng)建ajax
}catch(e){
xmlHttp = false;
} // try end
}else{ //Chrome、FireFox等非ie內(nèi)核
try{
xmlHttp = new XMLHttpRequest(); //視為非ie情況下
}catch(e){
xmlHttp = false; // 其他非主流瀏覽器
}
} // 判斷結(jié)束,如果創(chuàng)建成功則返回一個(gè)DOM對(duì)象,如果創(chuàng)建不成功則返回一個(gè)false
if(xmlHttp)
{
return xmlHttp;
}else{
alert("對(duì)象創(chuàng)建失敗,請(qǐng)檢查瀏覽器是否支持XmlHttpRequest!");
}
} // 函數(shù)體
//學(xué)院下拉框事件
function showCollegeInfo(){
var selectIndex = document.getElementById("college").selectedIndex;//獲得是第幾個(gè)被選中了
var value = document.getElementById("college").options[selectIndex].value;
if(value)
{
// 先創(chuàng)建一個(gè)對(duì)象實(shí)例
createXmlHttpRequestObject();
// 使用事件對(duì)象獲取文本框ID的值
var vCollege = value;
var url = "college.php?xy="+vCollege; //待發(fā)送URL
url=encodeURI(url);
xmlHttp.onreadystatechange=ajaxok; // 判斷瀏覽器狀態(tài)欄 (接收玩數(shù)據(jù)觸發(fā)的事件)
xmlHttp.open("GET",url,false); // GET向服務(wù)器端發(fā)送數(shù)據(jù)
xmlHttp.send(null);
document.getElementById("collegeinfo").style.display="block";//顯示學(xué)院信息的div
}else{
document.getElementById("collegeinfo").style.display="none";//隱藏學(xué)院信息的div
}
}
function ajaxok()
{
if(xmlHttp.readyState == 4 && xmlHttp.status==200)
{
document.getElementById("collegeinfo").innerHTML = xmlHttp.responseText;
}
}
//專(zhuān)業(yè)下拉框事件
function showMajorInfo(){
var selectIndex = document.getElementById("major").selectedIndex;//獲得是第幾個(gè)被選中了
var value = document.getElementById("major").options[selectIndex].value;
if(value)
{
// 先創(chuàng)建一個(gè)對(duì)象實(shí)例
createXmlHttpRequestObject();
// 使用事件對(duì)象獲取文本框ID的值
var vMajor = value;
var url = "major.php?zy="+vMajor; //待發(fā)送URL
url=encodeURI(url);
xmlHttp.onreadystatechange=ajaxok2; // 判斷瀏覽器狀態(tài)欄 (接收玩數(shù)據(jù)觸發(fā)的事件)
xmlHttp.open("GET",url,false); // GET向服務(wù)器端發(fā)送數(shù)據(jù)
xmlHttp.send(null);
document.getElementById("majorinfo").style.display="block";//顯示專(zhuān)業(yè)信息的div
}else{
document.getElementById("majorinfo").style.display="none";//隱藏專(zhuān)業(yè)信息的div
}
}
function ajaxok2()
{
if(xmlHttp.readyState == 4 && xmlHttp.status==200)
{
document.getElementById("majorinfo").innerHTML = xmlHttp.responseText;
}
}
</script>
前臺(tái)頁(yè)面兩個(gè)select框,一個(gè)與學(xué)院關(guān)聯(lián),另一個(gè)與專(zhuān)業(yè)關(guān)聯(lián),現(xiàn)需要選擇學(xué)院select框后,顯示學(xué)院相關(guān)信息,且專(zhuān)業(yè)select下面僅有屬于該學(xué)院的專(zhuān)業(yè)名稱。也就是實(shí)現(xiàn)一個(gè)二級(jí)聯(lián)動(dòng)效果。
兩個(gè)select里面分別定義onchange事件,事件中利用ajax的GET方法向后臺(tái)PHP遞交信息,再將查詢得到的信息echo出來(lái)或document.write。
注:代碼參考了有位叫y0umer的博主寫(xiě)的。
復(fù)制代碼 代碼如下:
<script type="text/javascript">
var XmlHttp;
function createXmlHttpRequestObject(){
if(window.ActiveXobject){ // 判斷是否是ie瀏覽器
try { // try開(kāi)始
xmlhttp = new ActiveXobject("Microsoft.XMLHTTP"); // 使用ActiveX對(duì)象創(chuàng)建ajax
}catch(e){
xmlHttp = false;
} // try end
}else{ //Chrome、FireFox等非ie內(nèi)核
try{
xmlHttp = new XMLHttpRequest(); //視為非ie情況下
}catch(e){
xmlHttp = false; // 其他非主流瀏覽器
}
} // 判斷結(jié)束,如果創(chuàng)建成功則返回一個(gè)DOM對(duì)象,如果創(chuàng)建不成功則返回一個(gè)false
if(xmlHttp)
{
return xmlHttp;
}else{
alert("對(duì)象創(chuàng)建失敗,請(qǐng)檢查瀏覽器是否支持XmlHttpRequest!");
}
} // 函數(shù)體
//學(xué)院下拉框事件
function showCollegeInfo(){
var selectIndex = document.getElementById("college").selectedIndex;//獲得是第幾個(gè)被選中了
var value = document.getElementById("college").options[selectIndex].value;
if(value)
{
// 先創(chuàng)建一個(gè)對(duì)象實(shí)例
createXmlHttpRequestObject();
// 使用事件對(duì)象獲取文本框ID的值
var vCollege = value;
var url = "college.php?xy="+vCollege; //待發(fā)送URL
url=encodeURI(url);
xmlHttp.onreadystatechange=ajaxok; // 判斷瀏覽器狀態(tài)欄 (接收玩數(shù)據(jù)觸發(fā)的事件)
xmlHttp.open("GET",url,false); // GET向服務(wù)器端發(fā)送數(shù)據(jù)
xmlHttp.send(null);
document.getElementById("collegeinfo").style.display="block";//顯示學(xué)院信息的div
}else{
document.getElementById("collegeinfo").style.display="none";//隱藏學(xué)院信息的div
}
}
function ajaxok()
{
if(xmlHttp.readyState == 4 && xmlHttp.status==200)
{
document.getElementById("collegeinfo").innerHTML = xmlHttp.responseText;
}
}
//專(zhuān)業(yè)下拉框事件
function showMajorInfo(){
var selectIndex = document.getElementById("major").selectedIndex;//獲得是第幾個(gè)被選中了
var value = document.getElementById("major").options[selectIndex].value;
if(value)
{
// 先創(chuàng)建一個(gè)對(duì)象實(shí)例
createXmlHttpRequestObject();
// 使用事件對(duì)象獲取文本框ID的值
var vMajor = value;
var url = "major.php?zy="+vMajor; //待發(fā)送URL
url=encodeURI(url);
xmlHttp.onreadystatechange=ajaxok2; // 判斷瀏覽器狀態(tài)欄 (接收玩數(shù)據(jù)觸發(fā)的事件)
xmlHttp.open("GET",url,false); // GET向服務(wù)器端發(fā)送數(shù)據(jù)
xmlHttp.send(null);
document.getElementById("majorinfo").style.display="block";//顯示專(zhuān)業(yè)信息的div
}else{
document.getElementById("majorinfo").style.display="none";//隱藏專(zhuān)業(yè)信息的div
}
}
function ajaxok2()
{
if(xmlHttp.readyState == 4 && xmlHttp.status==200)
{
document.getElementById("majorinfo").innerHTML = xmlHttp.responseText;
}
}
</script>
您可能感興趣的文章:
- Ajax+php數(shù)據(jù)交互并且局部刷新頁(yè)面的實(shí)現(xiàn)詳解
- php 接口與前端數(shù)據(jù)交互實(shí)現(xiàn)示例代碼
- 利用php做服務(wù)器和web前端的界面進(jìn)行交互
- Android App端與PHP Web端的簡(jiǎn)單數(shù)據(jù)交互實(shí)現(xiàn)示例
- 微信小程序?qū)W習(xí)筆記之表單提交與PHP后臺(tái)數(shù)據(jù)交互處理圖文詳解
- php變量與JS變量實(shí)現(xiàn)不通過(guò)跳轉(zhuǎn)直接交互的方法
- Ajax+PHP簡(jiǎn)單數(shù)據(jù)交互
- PHP與MySQL交互使用詳解
- PHP與服務(wù)器文件系統(tǒng)的簡(jiǎn)單交互
- PHP與Web頁(yè)面交互操作實(shí)例分析
相關(guān)文章
Ajax 返回字符串的過(guò)濾實(shí)現(xiàn)代碼
在調(diào)用Ajax返回后。一個(gè)奇怪的問(wèn)題。返回的resultString值是“ok”但是跟字符串"ok"比較確不相等。2009-08-08Ajax加載外部頁(yè)面彈出層效果實(shí)現(xiàn)方法
這篇文章主要介紹了Ajax加載外部頁(yè)面彈出層效果實(shí)現(xiàn)方法,涉及Ajax加載彈出層的實(shí)現(xiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05ajax實(shí)現(xiàn)上傳圖片保存到后臺(tái)并讀取的實(shí)例
下面小編就為大家分享一篇ajax實(shí)現(xiàn)上傳圖片保存到后臺(tái)并讀取的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01ajax提交到servelt獲取參數(shù)有亂碼的解決方法
這篇文章主要介紹了ajax提交到servelt獲取參數(shù)有亂碼的解決方法,需要的朋友可以參考下2014-02-02使用jquery 的ajax調(diào)用總是錯(cuò)誤親測(cè)的解決方法
使用jquery 的ajax功能調(diào)用一個(gè)頁(yè)面,卻發(fā)現(xiàn)總是出現(xiàn)錯(cuò)誤,經(jīng)過(guò)這么多測(cè)試終于正常了,尤其是 dataType: 'json',看來(lái)jquery有很?chē)?yán)格的驗(yàn)證機(jī)制2013-07-07AJAX實(shí)現(xiàn)跨域的三種方法(代理,JSONP,XHR2)
這篇文章主要介紹了AJAX實(shí)現(xiàn)跨域的三種方法(代理,JSONP,XHR2)的相關(guān)資料,需要的朋友可以參考下2016-03-03ajax傳遞多個(gè)參數(shù)的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了ajax傳遞多個(gè)參數(shù)的實(shí)現(xiàn)代碼,簡(jiǎn)單實(shí)用,感興趣的小伙伴們可以參考一下2016-05-05