使用json和php的多个条目

时间:2019-03-20 05:25:13

标签: javascript php json ajax

第一行中的值通过json成功显示。当我单击加上通过选择产品插入的新行时,它们甚至无法显示根据该相关产品的数据,甚至有时无法显示第一行数据。 这些是我的代码文件 第一个具有add_sale.php代码的文件 第二个文件具有过程代码。

<?php
include 'includes/db.php';
?>
<!DOCTYPE html>
<html>
<head>
<?php include 'includes/head.php' ?>
</head>

<body class="skin-default">
<div class="preloader">
    <div class="loader_img"><img src="img/loader.gif" alt="loading..." height="64" width="64"></div>
</div>
<!-- header logo: style can be found in header-->
<?php include 'includes/header.php' ?>
<div class="wrapper row-offcanvas row-offcanvas-left">
    <!-- Left side column. contains the logo and sidebar -->
<?php include 'includes/menu.php' ?>

<aside class="right-side">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>Add Sales</h1>
        <ol class="breadcrumb">
            <li>
                <a href="index-2.html">
                    <i class="fa fa-fw ti-home"></i> Dashboard
                </a>
            </li>
            <li> Pages</li>
            <li class="active">
                <a href="blank.html">Blank</a>
            </li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
<div class="container" >
  <div class="box-header with-border">
    <h2  class="font-weight-bold mb-1"><b> <i>SALES INFORMATION</i></b></h2><br>
  </div>
<br>
<form method="POST" action="" >
<div class="row">
    <div class="col-md-10">

        <table   class="table table-hover small-text" id="tb">
            <tr class="tr-header">
              <th>Item</th>
              <th>Price</th>
              <th>Quantity</th>
              <th>Total Price</th>
              
              <th><a href="javascript:void(0);" style="font-size:18px;" id="addMore" title="Add More Person"><span class="glyphicon glyphicon-plus"></span></a></th>

            <tr class="tr-body">
                <td>
                    <select id="product"  name="" class="form-control">
                    <option value="0">Choose</option>
                       <?php
                       $select_items = "SELECT * FROM stock_items";
                       $run_items = mysqli_query($connection,$select_items);
                       while ($row1=mysqli_fetch_array($run_items)) {?>
                         <option  value="<?php echo $row1['id']; ?>"> <?php echo $row1['title']; ?> </option>
                      <?php } ?>
                    </select>
                </td>
                <td><input type="text" name=""   id="price"  class="form-control"></td>
                <td><input type="text" name="" id="quantity"    class="form-control"></td>
                <td><input type="text" name="" id="total"    class="form-control"></td>
              
                <td><a href='javascript:void(0);'  class='remove'><span class='glyphicon glyphicon-remove'></span></a></td>
            </tr>
        </table>

    </div>
</div>
<br>
    <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-3">
            <input type="submit" name="save" value="Save" class="btn btn-primary btn-md">
            <a href="dashboard.php" class="btn btn-danger btn-md ">CANCEL </a>
        </div>
    </div>    
</div>
</form>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">


$(document).ready(function(){


});

    // Adding More Rows
$(function(){
    $('#addMore').on('click', function() {
            var data = $("#tb tr:eq(1)").clone(true).appendTo("#tb");
            data.find("input").val('');
            });
    $(document).on('click', '.remove', function() {
       var trIndex = $(this).closest("tr").index();
          if(trIndex>1) {
           $(this).closest("tr").remove();
         } else {
           alert("Sorry!! Can't remove first row!");
         }
    });
}); 

// For Total Price
$("#quantity").keyup(function(){

    var totalsum= $('#price').val();
    var quantity=$('#quantity').val();
    var due = totalsum * quantity;
    $('#total').val(due);

});

$('#product').change(function()
{
    var product = $('#product').val();
    if(product == 0 )
    {

    }else 
    {
        $.ajax({
        type : 'POST',
        url : 'add_sales_process.php',
        dataType:"json",
        data  : {product_id:product},
        success : function (data)
            {
                
                $('#price').val(data.price);      
                $('#quantity').val(data.qunantity);    
                //$(".tr-body").find("tr").html(data);
            }
        });
    }
});
</script>
<?php include 'includes/right-menu.php' ?>
</div>
<script src="js/app.js" type="text/javascript"></script>
</body>
</html>

这是发生json编码数据的过程文件。

<?php
include 'includes/db.php';

if(isset($_POST['product_id']))
 {
	$product_id= $_POST['product_id'];
	
	
	$select_item = "SELECT * FROM stock_items WHERE id = '$product_id' ";
	$run_item = mysqli_query($connection,$select_item);
	while($row=mysqli_fetch_array($run_item))
	{
		
		$fetch_id = $row['id'];
		$title = $row['title'];
		$baroce = $row['barcode'];
		$quantity = $row['quantity'];
		$sale_price = $row['sale_price'];
		$status = $row['status'];

		$data = ["title" => $title, "price" => $sale_price, "qunantity" => $quantity ];

	}
	echo json_encode($data);

}
?>

以下是输出图像:
enter image description here

0 个答案:

没有答案