首頁  >  文章  >  後端開發  >  php如何實作ajax請求

php如何實作ajax請求

L
L原創
2020-06-01 10:18:383668瀏覽

php如何實作ajax請求

php實作ajax的接收    

Ajax介面:

#首先,要理解本質,就是普通的一個提交在無刷新的情況下發出請求後得到回應,然後去針對你需要

的情況來做行為。

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
  }
xmlhttp.open("GET","send.php?name=sunzhiyan",true);
xmlhttp.send();
}
</script>
</head>
<body> <div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button> </body>
</html>

PHP介面:

這裡了注意,如果是透過get方法傳遞的一定要透過$_GET方法才能夠接受值,反之則$_POST

<?php
if($_GET[&#39;name&#39;] == sunzhiyan){
 echo &#39;this is right&#39;;
}else{
 echo &#39;this is error&#39;;
} 
?>

這是簡單的一個Ajax的運用,能夠實現Ajax的驗證。

推薦教學:《PHP教學

以上是php如何實作ajax請求的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn