weixin_33704234 2016-02-05 19:16 采纳率: 0%
浏览 20

WordPress和Ajax

I've have created a custom table in mysql. I've entered a bit of test data just to see if I can pull some results. The query was successful when I ran it just in the template, so I know it works. As I have tried to convert it into a Ajax request seems that no data is being passed. I'm not sure what it is I am missing, or possibly an error I have entered somewhere, but it seems when I try to turn it into an Ajax request nothing happens.

Any ideas?

PHP

    $q = intval($_GET['q']);

$pull = $wpdb->get_results( 
    "
    SELECT ID,  department, contact, forms
    FROM referrals
    WHERE ID = '$q'
    "
);

foreach ( $pull as $requestdata) 
{
    echo $requestdata->department;
    echo '<br />';
    echo $requestdata->contact;
    echo '<br />';
    echo $requestdata->forms;
}

AJAX

<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>

HTML

                                <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>
  • 写回答

2条回答 默认 最新

  • weixin_33709609 2016-02-05 19:30
    关注

    You need to change your ajax request to wp-admin/admin.ajax.php.

    If you open that file you will find there is a constant setting named DOING_AJAX. Only the requests to this link can avoid the normal header being sent to browser because of setting DOING_AJAX to ture.

    Actually you can debug like this: visit <?php echo content_url(); ?>/themes/child/get-referral.php?q=xx, you will find there are other info sent. thats why that ajax doesnt work.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?