簡體   English   中英

從PHP返回json數組到jQuery AJAX

[英]Return json array from PHP to jQuery AJAX

我正在嘗試做一些實驗。 我想按產品名稱在MySQL數據庫中搜索產品,並在“價格輸入”中檢索價格,在“賣價輸入”中出售價格。 示例圖片

一切正常,除了將fetch.php數組檢索為javascript變量以獲取price和sellprice輸入值。 我試圖在php文件中使用json_encode來獲取javascript中的php數組,但仍然無法正常工作。 我對javascript不太了解。 可能我沒有正確編碼,這就是為什么javascript無法獲取php數組的原因。 任何幫助將不勝感激。 謝謝 :)

索引

<html>
        <head>
            <script src="js/jquery.js"></script>
        </head>
        <body>
            <div class="productdetail">
                <table class="productdetail">
                    <th>Product</th>
                    <th>Price</th>
                    <th>Sell price</th>
                    <tr>
                        <td><input class='product' type='text' name='product' /></td>
                        <td><input class='price' type='text' name='price' /></td>
                        <td><input type='text' class='sellprice' name=''/></td>
                    </tr>
                </table>
    <div id="result"></div>
        </body>

    </html>
    <script>
    $(function(){

            $('.productdetail').delegate('.product,.price,.sellprice','keyup',function(){
                var tr = $(this).parent().parent().parent();
                var product = tr.find('.product').val();
                if(product != '')  
               {  
                    $.ajax({  
                         url:"fetch.php",  
                         method:"post",  
                         data:{product:product},  
                         dataType:"text",  
                         success:function(data)  
                         {  
                    console.log(price);
                    var price = jQuery.parseJSON(price);

                    console.log(sellprice);
                    var sellprice = jQuery.parseJSON(sellprice);

                    //var price = "test price";
                    //var sellprice = "test sell price";

                     tr.find('.price').val(price);
                     tr.find('.sellprice').val(sellprice);

                    $('#result').html(data);  
                         }  
                    });  
               } 

            }); 
        });     
    </script>

fetch.php

<?php

$product = strtolower($_POST["product"]);

require_once ('db.php');
$sql = "SELECT * FROM tbproduct WHERE product LIKE '$product'";

$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {        {


            $array = array(
                'price' => $row["price"],
                'sellprice' => $row["sellprice"],
                );
                echo json_encode($array);
        }

    }
} else {
    echo 'Data Not Found';
}

?>

我知道了。 現在工作:)

$(function(){

        $('.productdetail').delegate('.product,.price,.sellprice','keyup',function(){
            var tr = $(this).parent().parent().parent();
            var product = tr.find('.product').val();
            if(product != '')  
           {  
                $.ajax({  
                     url:"fetch.php",  
                     method:"post",  
                     data:{product:product},  
                     //dataType:"json",  
                     success:function(data)  
                     {  

                    var getarray = jQuery.parseJSON(data);
                    var price = getarray.price;
                    var sellprice = getarray.sellprice;

                 tr.find('.price').val(price);
                 tr.find('.sellprice').val(sellprice);

                //$('#result').html(data);  
                }

                });  
           } 

        }); 
    });

嘗試用數據替換ajax中的price參數,例如

 $.ajax({ url:"fetch.php", method:"post", data:{product:product}, dataType:"text", success:function(data) { console.log(data); var price = jQuery.parseJSON(data); 
然后控制台數據,我認為您想要的兩個數組是data [0]和data [1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM