[英]Call php file using AJAX
我試圖使用本指南來使用AJAX對表單進行更改。 它無法正常工作,我在排除故障時遇到困難。 我當前的代碼:
基本頁面
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
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 (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","demo_find_location.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="countries" onchange="showUser(this.value)">
<option value="">Select a country:</option>
<option value="1">USA</option>
<option value="2">Canada</option>
<option value="3">China</option>
<option value="4">Viet Nam</option>
</select>
</form>
<br>
<div id="txtHint"><b>Country info will be listed here.</b></div>
</body>
</html>
demo_find_location.php
<?php
$q = intval($_GET['q']);
echo $q;
?>
由於選擇選項value = ""
時將重置id="textHint"
的div,因此會觸發onchange函數。 它似乎沒有處理我的php文件中的php。 可能是路徑問題或代碼不兼容? 我目前正在使用/ var / www / html / sites / all / libraries目錄包含我的php文件。
您的代碼工作正常,只是在使用javascript / jquery代碼時不要忘記在標題中加載jquery庫
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
/// rest of your page
另外,這是您目前正在嘗試執行的操作的精簡版,希望它可以幫助您了解jquery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
/// We trigger the function onchange
function showUser()
{
// We get the value of the country
var q = document.getElementById("countries").value;
if(!isNaN(q))//We check if the value is a valid number
{
$("#txtHint").load("demo_find_location.php?q="+q);//We load the result in txtHint
}
}
</script>
<form>
<select id="countries" onchange="showUser()">
<option value="">Select a country:</option>
<option value="1">USA</option>
<option value="2">Canada</option>
<option value="3">China</option>
<option value="4">Viet Nam</option>
</select>
</form>
<br>
<div id="txtHint"><b>Country info will be listed here.</b></div>
這個值是否為空? 如果該函數被onchange事件調用並且發短信被重置,那是因為傳遞的string(str)=“”。
<!DOCTYPE html>
<html>
<head>
<script>
var baseUrl="this is your website path";
//
// http://localhost/websitefolder/path or http://livepath
//
var eleTxtHint = document.getElementById("txtHint");
function showUser(str)
{
eleTxtHint.innerHTML="";
if (str!="")
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
} else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (this.readyState==4 && this.status==200) {
var response=JSON.parse(this.responseText);
if(response.status==true)
{
eleTxtHint.innerHTML=this.responseText;
}
else
{
alert('somethings went wrong');
}
}
else
{
alert('somethings went wrong');
}
};
xmlhttp.open("GET",baseUrl+"demo_find_location.php?q="+str,true);
xmlhttp.send();`enter code here`
}
}
</script>
</head>
<body>
<form>
<select name="countries" onchange="showUser(this.value)">
<option value="">Select a country:</option>
<option value="1">USA</option>
<option value="2">Canada</option>
<option value="3">China</option>
<option value="4">Viet Nam</option>
</select>
</form>
<br>
<div id="txtHint"><b>Country info will be listed here.</b></div>
</body>
</html>
demo_find_location.php
<?php
$q = isset($_GET['q'])?intval($_GET['q']):"";
echo json_encode(array('status'=>true,'data'=>$q));
exit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.