dorisdong0514 2013-12-11 17:22
浏览 54
已采纳

使用ajax调用时,不会填充MySQL查询数组

I'm using ajax to return the results of a sql query on a WordPress site. Basically, there are two queries and I am displaying the results of the second query. The first query gets a list of posts that contain a certain custom field. The second query looks through those posts and lists out the posts that have another custom field associated with them. Right now, if I don't use ajax and just add this to my page template, everything works.

<?php
    $post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'Make' AND meta_value='Chevrolet'" );
    // now get the food names
    $stocktypes = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'Model' AND post_id IN (". implode(',', array_map('absint', $post_ids) ) .")" );


    if ($stocktypes) {
        foreach ($stocktypes as $stocktype) {
            echo "<option value=\"" . $stocktype . "\">" . $stocktype . "</option>";
        }
    }
?>

The problem comes when I add that to a separate file and call it with ajax. Here is the contents of my ajax file:

<?php

    $models = $_GET['Make'];

    $post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'Make' AND meta_value= %s", $models );

    $stocktypes = $wpdb->get_col( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'Model' AND post_id IN (". implode(',', array_map('absint', $post_ids) ) .")" );


    if ($stocktypes) {
        foreach ($stocktypes as $stocktype) {
            echo "<option value=\"" . $stocktype . "\">" . $stocktype . "</option>";
        }
    }
?>

Here is my ajax call:

 <script>
    $(document).ready(function(){
        $("#make").change(function() {
            $.ajax({ 
                url: "<?php bloginfo('template_url'); ?>/getModel.php", 
                data: { Make: $(this).val() },
                dataType: "html",
                type: "GET", 
                success: function(data){ 
                    $("#model").html(data); 
                } 

            }); 
        });
    });
</script>

The ajax call is working fine because I can echo the results of $models = $_GET['Make'];

By using print_r(array_values($post_ids));, I found that the $post_ids array of the first query isn't being populated in my ajax file. The only thing I could think of is that wp-load.php (which allows for the use of WordPress functions) wasn't being loaded but that's not the problem.

  • 写回答

1条回答 默认 最新

  • duanmu2013 2013-12-11 17:43
    关注

    The problem was in the first query. While I was using meta_value= %s, I should have been using meta_value= $models

    Also, as pointed out by Kevin B, I added type: "GET" to my ajax call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?