首页 > 解决方案 > 将元素存储到数据库中

问题描述

我有一个动态创建的表。意味着标题是固定的,并且行数据将在某些 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>

标签: phplaravellaravel-5

解决方案


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

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

推荐阅读