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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀