如何解析jQuery中的JSON数据并追加到表行

时间:2018-10-20 10:38:54

标签: php jquery json

我已成功将表单数据提交到数据库。如果查询运行,它将选择插入的最后一行,并应作为json数据返回并追加到视图表。我找不到如何解析jQuery成功功能上的json数据 这是我的代码

$.ajax({
        type: 'POST',
        url: form.attr("action"),
        data: $("#myForm input").serialize(),
        dataType: 'json',
        success: function(data) {
        //alert("Branch Added Successfully");.
        if(data == 0){
            alert('Product Already Exists');
         }else{
             var markup = "<tr><td><input type='checkbox' name="+ data.enq_id +"></td><td>" + data.category + "</td><td>" + data.brand + "</td><td>" + data.product_name + "</td><td><i class='fa fa-inr'></i> " + data.price + ".00 </td><td>" +
                         data.quantity + "</td><td><i class='fa fa-inr'></i> " + data.totalprice + " .00</td></tr>";
            $("table tbody").append(markup);
            var frm = document.getElementsByName('myForm')[0];
            // Submit the form
            frm.reset();  // Reset all form data
            return false;
               }
            }
     });

这是操作页面,即insert_inquiry_product.php

<?php
include('../../config.php');
extract($_POST);
if(isset($_POST['submit'])) {
    //echo "success";
    $date=date('d-m-Y');
    $sql="select *  from enquiry_product where product_name='$productname' and enq_no='$enq_no'";
    $fire=mysqli_query($link,$sql);
    if(mysqli_num_rows($fire) > 0){
        echo(0);
    }else{
        $query = "INSERT INTO `enquiry_product`(`enq_no`, `category_id`, `brand_id`, `product_name`, `price`, `quantity`, `total_price`) VALUES ('$enq_no','$category','$brand','$productname','$hid_mrp','$quantity','$hid_total')";
        //echo $query; die();
        $run_query=mysqli_query($link,$query);
        if($run_query){
            /**/
            //echo "<script>alert('Product Added successfully!');window.location='product.php'</script>";
            $selectquery="select `product_category`.`id` as `p_id` , `product_category`.`category_name` as `p_name`, `brand`.`id` as `b_id` ,`brand`.`brand_name` as `b_name` , `enquiry_product`.`id` as `en_id`, `product_name` ,`price`, `quantity`,`total_price` from `enquiry_product` inner join `brand` on `enquiry_product`.`brand_id` = `brand`.`id` left outer join `product_category` on `brand`.`cat_id` = `product_category`.`id` where `enquiry_product`.`enq_no` = '101' ORDER BY `enquiry_product`.`id` DESC LIMIT 1";
            //echo $selectquery; die();
            $selfire =mysqli_query($link,$selectquery);
            $res=mysqli_fetch_assoc($selfire);
            $en_id = $res['en_id'];
            $categ_name=$res['p_name'];
            $brand_name=$res['b_name'];
            $pro_name=$res['product_name'];
            $sprice=$res['price'];
            $quant=$res['quantity'];
            $t_price=$res['total_price'];

            echo json_encode(
            array("enq_id" => $en_id ,"category" => $categ_name, "brand" => $brand_name , "product_name" => $pro_name, "price" => $sprice , "quantity" => $quant , "totalprice" => $t_price));
        }
        else{
            echo "Error in query";
        }
        }
    }
else{ echo "button is not set"; }?>

0 个答案:

没有答案