Javascript Wordpress和Ajax
我已经在mysql中创建了一个自定义表。我输入了一些测试数据,只是想看看我是否能得出一些结果。当我在模板中运行查询时,它是成功的,所以我知道它是有效的。当我试图将其转换为Ajax请求时,似乎没有传递任何数据。我不确定我遗漏了什么,或者可能是我在某处输入了一个错误,但当我试图将其转换为Ajax请求时,似乎什么都没有发生 有什么想法吗 PHPJavascript Wordpress和Ajax,javascript,php,ajax,wordpress,Javascript,Php,Ajax,Wordpress,我已经在mysql中创建了一个自定义表。我输入了一些测试数据,只是想看看我是否能得出一些结果。当我在模板中运行查询时,它是成功的,所以我知道它是有效的。当我试图将其转换为Ajax请求时,似乎没有传递任何数据。我不确定我遗漏了什么,或者可能是我在某处输入了一个错误,但当我试图将其转换为Ajax请求时,似乎什么都没有发生 有什么想法吗 PHP $q=intval($\u GET['q']); $pull=$wpdb->获取结果( " 选择ID、部门、联系人、表单 从转介 其中ID='$q' " );
$q=intval($\u GET['q']);
$pull=$wpdb->获取结果(
"
选择ID、部门、联系人、表单
从转介
其中ID='$q'
"
);
foreach($pull as$requestdata)
{
echo$requestdata->department;
回声“
”;
echo$requestdata->contact;
回声“
”;
echo$requestdata->forms;
}
阿贾克斯
函数显示数据(str){
如果(str==“”){
document.getElementById(“txt”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txt”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,”/themes/child/GET-reflection.php?q=“+str,true);
xmlhttp.send();
}
HTML
选择一个人:
测试1
您需要将ajax请求更改为wp admin/admin.ajax.php
如果打开该文件,您会发现有一个名为DOING\u AJAX
的常量设置。由于将DOING\u AJAX
设置为true,因此只有对该链接的请求才能避免将正常标头发送到浏览器
实际上你可以这样调试:访问/themes/child/get reference.php?q=xx
,你会发现还有其他信息发送。这就是为什么ajax不起作用。切换到这个。希望它能帮助其他人
HTML
挑选
选择权
其他人
其他人
试验
AJAX/JS
<script>
function pullData() {
var xhttp;
//url variables
var url = "/wp-content/referrals/";
var selectedValueIs = document.getElementById("mySelect").value;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
}
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("referral").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", url+selectedValueIs+'.txt' , true);
xhttp.send();
}
</script>
函数pullData(){
var-xhttp;
//url变量
var url=“/wp content/referrals/”;
var selectedValueIs=document.getElementById(“mySelect”).value;
if(window.XMLHttpRequest){
xhttp=newXMLHttpRequest();
}
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
document.getElementById(“参考”).innerHTML=xhttp.responseText;
}
};
xhttp.open(“GET”,url+selectedValueIs+'.txt',true);
xhttp.send();
}
谢谢。这不只是为了管理员吗?@RWHammond不,ajax是公共方法谢谢zairwolf的回复。谢谢。无法让它工作。我只是想调用一个文件,而不是db查询。似乎有效。
<script>
function displayData(str) {
if (str=="") {
document.getElementById("txt").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txt").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","<?php echo content_url(); ?>/themes/child/get-referral.php?q="+str, true);
xmlhttp.send();
}
</script>
<select name="users" onchange="displayData(this.value)">
<option value="">Select a person:</option>
<option value="1">Test 1</option>
<option value="2"Test 2</option>
</select>
<br>
<div id="txt"></div>
<select id="mySelect" onchange="pullData()">
<option value="">Select</option>
<option value="options">options</option>
<option value="someother1">someother1</option>
<option value="someother_2">someother_2</option>
</select>
<div id="referral">Test</div>
<script>
function pullData() {
var xhttp;
//url variables
var url = "/wp-content/referrals/";
var selectedValueIs = document.getElementById("mySelect").value;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
}
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("referral").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", url+selectedValueIs+'.txt' , true);
xhttp.send();
}
</script>