使用ajax下拉多个数据,json无效

时间:2016-07-21 02:44:40

标签: php json ajax

我正在尝试从数据库中提取数据,下面的代码工作正常,如果批号在数据库中只有一条记录,则显示批号的下拉,但是当我在同一批号中添加更多库存时,它会停止显示下拉批号,另一方面,如果我做相同的代码没有json它显示所有的批号但我想用json处理这个因为我还要从db中提取一些其他数据,我将在其他一些显示divs,请帮助我做错了。

的JQuery / AJAX

<script>
    function getBatch(id) {

        var product_id = jQuery('#product'+id).val();
        jQuery.ajax({
            type: 'POST',
            url: 'get_Batch.php',
            data: { product_id: product_id },
            dataType : 'JSON',
            success: function(data) {
                jQuery('#batch'+id).html(data.batch_option);
                jQuery('#available_qty'+id).val(data.avaliable_quantity);
            }
        });

    }
</script>

PHP

<?php

include('includes/config.php');

if(isset($_POST['product_id'])) :

    $product_id = $_POST['product_id'];
    $get_batch = mysqli_query($conn, "Select * from products JOIN products_stock ON products.p_id='$product_id' AND products.p_id=products_stock.p_id");
    if(mysqli_num_rows($get_batch) > 0) :
        header('Content-Type: application/json');
        while($batch = mysqli_fetch_object($get_batch)) :
            $data = [
                        'batch_option'          =>  "<option value=".$batch -> batch_no."> 
                                                        ".$batch -> batch_no."
                                                    </option>",
                        'avaliable_quantity'    =>  $batch -> p_available
                    ];
            echo json_encode($data);
        endwhile;
    else :
        header('Content-Type: application/json');
        $data = [
                    'batch_option'  =>  "<option value='No batch found'> No batch found </option>",
                    'avaliable_quantity'    =>  'Not avaliable'
                ];
        echo json_encode($data);
    endif;
endif;

&GT;

如果我喜欢这样,它将在下拉列表中显示所有记录与一个批号

<?php

include('includes/config.php');

if(isset($_POST['product_id'])) :

    $product_id = $_POST['product_id'];
    $get_batch = mysqli_query($conn, "Select * from products JOIN products_stock ON products.p_id='$product_id' AND products.p_id=products_stock.p_id");
    if(mysqli_num_rows($get_batch) > 0) :
        while($batch = mysqli_fetch_object($get_batch)) :
        ?> <option value="<?php echo $batch -> id; ?>"><?php echo $batch -> id; ?></option> <?php
        endwhile;
    endif;
endif;

&GT;

1 个答案:

答案 0 :(得分:1)

更改while循环,以便在其外定义$data,并将结果附加到循环中的$data

header('Content-Type: application/json');
$data = []; //will store results
while($batch = mysqli_fetch_object($get_batch)) :
    $data[] = [...];    //add next result       
endwhile;
echo json_encode($data); //encode and return all results.

在Javascript中,您还必须更改填充HTML的方式。您当前的代码只会将data.batch_optiondata.avaliable_quantity添加到您的文档中。但是,如果您有多个结果,data将是一个数组,因此您不会遍历其成员并将每个成员添加到文档中。

success: function(data) {
    //loop through each result in data
    data.forEach(function(result){
        var opt = result.batch_option;
        var qty = result.avaliable_quantity;

        //Todo: add opt and qty to the right DOM elements
    });
}