【问题标题】:How to replace initial PHP variable with new value on select in AJAX call?如何在 AJAX 调用中用新值替换初始 PHP 变量?
【发布时间】:2014-08-10 20:02:56
【问题描述】:

我正在运行 AJAX 调用,它将选择选项值(从状态下拉列表中)传递到 Wordpress 查询中。

这是我的 AJAX 调用:

$.ajax({
    url:"<?php bloginfo('template_directory'); ?>/inc/businesses.php",
    type: 'POST',
    data: {postID: '<?php the_ID(); ?>', postState: $("#state").val()},
    success: function(resp) {
        $('#results').html(resp);
    }
});

这是来自businesss.php的部分代码:

<?php 
require('../../../../wp-load.php');
$postState = $_POST['postState'];

$userState = geoCheckIP($ip);
?>
<?php 
$args = array(
    'post_type' => 'businesses',
    'orderby' => 'date',
    'order' => "ASC",
    'meta_query' => array(
        array(
            'key' => 'state',
            'value' => $userState,
            'compare' => '=', 
            'type' => 'CHAR' 
        )
    )
);

// Query
$the_query = new WP_Query( $args );
?>

基本上我想在页面加载时将$userState 传递到查询参数中,然后在更改选择下拉菜单时将$userState$postState 交换。

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:

    也许这行得通?

    'value' => (isset($_POST['postState']) ? $_POST['postState'] : $userState),
    

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多