繁体   English   中英

AJAX 从 PHP 接收多个结果

[英]AJAX receives multiple results from PHP

我希望有人可以帮助对其进行排序,AJAX 只收到一个结果显示在 select 下拉列表中。 它看起来像这个 Model1Model2Model3Model4。 我想看起来像这样

型号1

模型2

模型3

型号4

在 jquery 脚本上看起来像这样:

$('#input_11_183').append('Model1');

$('#input_11_183').append('Model2');

$('#input_11_183').append('Model3');

$('#input_11_183').append('Model4');

所有这些数据都将添加到 select 下拉字段中

这是我的 php 代码:

<?php
function list_of_brandcars() {
    $model_option = $_POST['pass_data'];  

    $carposts = array(             
        'post_type' => 'list_of_cars',
        'post_status'    => 'publish',              
        's'     => $model_option
        );                  

    $att = new WP_Query($carposts);
    $count=0;
    if($att->have_posts()){
    
        while($att->have_posts()) : $att->the_post();                                
                while(have_rows('mods')) : the_row();                                                             
                    echo get_sub_field('model');                   
                endwhile;                  
        endwhile;

    }
    die(); 
}
add_action('wp_ajax_nopriv_list_of_brandcars', 'list_of_brandcars');
add_action('wp_ajax_list_of_brandcars', 'list_of_brandcars');
?>

这是我的 jQuery 脚本

<script>
$(document).ready(function($) { 
    
    $('#input_11_11').change(function(){
        var from_brand = $(this).val();
        
        $.ajax({
            type: 'POST',
            url: ajaxurl,   
            data: {     
                action: 'list_of_brandcars',                        
                pass_data: from_brand
            },
            success: function(data) {           
                $('#input_11_183').empty();
                for (var i = 0; i < data.length; i++) {             
                    $('#input_11_183').append('<option value="' + data + '">' + data + '</option>');
                }
            }
        });
        die();
    });
});
</script>

您正在输出原始字符串,它们只是连接在一起并成为响应中的单行文本。 您需要 output JSON 格式的一些结构化数据。 为此,首先创建一个数组,然后将每段文本添加到数组中。

这是一个例子。 我在代码中添加或更改某些内容的所有地方都添加了注释:

PHP

$att = new WP_Query($carposts);
$count=0;
$response = array(); //make empty array

if($att->have_posts()){
    while($att->have_posts()) : $att->the_post();                                
            while(have_rows('mods')) : the_row();                                                             
                $response[] = get_sub_field('model'); //add to the array
            endwhile;                  
    endwhile;
}

echo json_encode($response); //encode the array as JSON and output it
die();

JavaScript:

    $.ajax({
        type: 'POST',
        url: ajaxurl,
        dataType: "json", //tell jQuery to expect JSON in the response, and automatically parse it
        data: {     
            action: 'list_of_brandcars',                        
            pass_data: from_brand
        },
        success: function(data) {           
            $('#input_11_183').empty();
            for (var i = 0; i < data.length; i++) {             
                $('#input_11_183').append('<option value="' + data[i] + '">' + data[i] + '</option>'); //access the correct element of (what is now) an array of strings.
            }
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM