php開發之AJAX一,php開發之AJAX

來源:互聯網
上載者:User

php開發之AJAX一,php開發之AJAX

AJAX這個東東前兩天剛剛接觸到,聽別人說挺有用的哦,覺的有必要學一下。
下面就通過get方法傳遞參數到php頁面為例來說一下:
test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>test</title><script type ="text/javascript" src ="test.js"></script></head><body><input type ="button" value ="submit" onclick ="testcheck()"></body></html>

test.js

var xmlHttp;function GetXmlHttpObject(){    if (window.XMLHttpRequest){      // code for IE7+, Firefox, Chrome, Opera, Safari      xmlhttp=new XMLHttpRequest();    }else{// code for IE6, IE5      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");    }    return xmlhttp;}// -----------ajax方法-----------//function testcheck(){    xmlHttp=GetXmlHttpObject();    if (xmlHttp==null){        alert('您的瀏覽器不支援AJAX!');        return;    }    var url="http://localhost/PHPTEST/index.php?isbn=110120";//這個地方一定要注意哦,剛開始的時候我在‘=’前面多了一個空格,結果造成了php頁面擷取不到參數,大家都一定要注意些哦    xmlHttp.open("GET",url,true);    xmlHttp.onreadystatechange=getOkGet;//發送事件後,收到資訊了調用函數    xmlHttp.send();}function getOkGet(){    if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){        // 本地提示:載入中    }    if (xmlHttp.readyState==4 && xmlHttp.status==200){       alert(xmlHttp.responseText);       alert('123');        // 處理返回結果    }}

index.php

<?php header('Content-type:text/html');if(isset($_GET['isbn'])){    $isbn =$_GET['isbn'];    if(!$isbn){        echo("abc");    //  print "That request was not understood";    }else if ($isbn =="110120") {        echo($isbn);    //  print "JavaScript step by step";    }}else{    echo("aaa");}?>

另外test.js的url應該是你的檔案所在的路徑,別弄錯了哦,嘿嘿
下面給大家看看運行效果哦



demo地址如下:demo

迫切希望能遇到一位大牛給予指導啊!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.