简体   繁体   English

从PHP返回json数组到jQuery AJAX

[英]Return json array from PHP to jQuery AJAX

Im trying to do something experimental. 我正在尝试做一些实验。 i would want to search product in MySQL database by product name and retrieve price in 'price input', sell price in 'sellprice input'. 我想按产品名称在MySQL数据库中搜索产品,并在“价格输入”中检索价格,在“卖价输入”中出售价格。 example picture 示例图片

Everything is ok, except retrieve fetch.php array as javascript variable to get price and sellprice input value. 一切正常,除了将fetch.php数组检索为javascript变量以获取price和sellprice输入值。 i tried to use json_encode in php file to get php array in javascript, still dosen't working. 我试图在php文件中使用json_encode来获取javascript中的php数组,但仍然无法正常工作。 I don't have much knowledge about javascript. 我对javascript不太了解。 Probably I didn't coded properly thats why javascript not getting php array. 可能我没有正确编码,这就是为什么javascript无法获取php数组的原因。 Any kind of help would be appreciated. 任何帮助将不胜感激。 Thanks :) 谢谢 :)

index.htm 索引

<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 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';
}

?>

i got it. 我知道了。 Working now :) 现在工作:)

$(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);  
                }

                });  
           } 

        }); 
    });

Try to replace the price argument in ajax with data eg 尝试用数据替换ajax中的price参数,例如

 $.ajax({ url:"fetch.php", method:"post", data:{product:product}, dataType:"text", success:function(data) { console.log(data); var price = jQuery.parseJSON(data); 
Then console the data I think that the two arrays that you want is the data[0] and data[1] 然后控制台数据,我认为您想要的两个数组是data [0]和data [1]

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

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