将元素存储到数据库中

时间:2018-06-14 03:22:53

标签: php laravel laravel-5

我有一个动态创建的表。表示标头是固定的,行数据将在某个ajax调用后动态追加。我的问题是如何在数据库中使用product_id(<tr> id)保存数量值?

 <tr> 
   <th></th>                   
   <th>Product Name</th>
   <th>Unit Price</th>
   <th>Quantity</th>                   
   <th>Total </th>
 </tr>

//在我使用自动完成搜索的ajax调用后,此部分将附加

 <tr id="+ui.item.id+">   // product_id
    <td><i class='flaticon-delete-1 delete-row' onclick='deleteRow(this)'></i></td>
     <td>"+ui.item.value+"</td>
     <td>"+ui.item.unit_price+"</td>
     <td><input type='text' class='quantity' value='1'></td> //quantity
     <td class='total'>"+ui.item.unit_price+"</td>
  </tr>

3 个答案:

答案 0 :(得分:1)

将数量和产品存储在数组中:

     <td><input type='text' name="id[]" class='productclass' value='+ui.item.id+'></td>
     <td><input type='text' name="quantity[]" class='quantity' value='1'></td>

如果您不想显示产品ID,可以隐藏:

<input type='hidden' name="id[]" class='productclass' value="+ui.item.id+">

在服务器端:

//Check input data
if(in_array("",$_POST['id'], true)){
//Empty id
}else{
  $countid = count($_POST['id']);
  for($j=0;$j<$countid;$j++){
      $id = $_POST['id'][$j];
      $quantity = $_POST['quantity'][$j];
      //code for saving into the database
  }
}

答案 1 :(得分:0)

尝试这样做。对于这个特别选择参考ID明智。

答案 2 :(得分:0)

  

如何做到这一点(只需用固定值替换你的 ui.item

<table>
     <tr class="product-list" id="100">
        <td><i class='flaticon-delete-1 delete-row onclick='deleteRow(this)'></i></td>
         <td>Product 1</td>
         <td>200</td>
         <td><input type="text" class="quantity" value="1"></td>
         <td class='total'>200</td>
      </tr>

      <tr class="product-list" id="101">
        <td><i class='flaticon-delete-1 delete-row onclick='deleteRow(this)'></i></td>
         <td>Product 2</td>
         <td>250</td>
         <td><input type="text" class="quantity" value="1"></td>
         <td class='total'>250</td>
      </tr>
</table>
<button type="button" onclick="postData()">Post</button>
  

使用jQuery

function postData(){
    var products = [];
    $('.product-list').each(function(index, el){

      var productId = $(this).attr('id');
      var quantity = $(this).find('.quantity').val();
      var total = $(this).find('.total').text();
      products[index]['id'] = productId;
      products[index]['quantity'] = quantity;
      products[index]['total'] = total;
    });

  //now post $products through AJAX


}