I have a dynamically created table. Means the header is fixed and the row data will append dynamically after certain ajax call. My question is how to save quantity value with product_id (which is the <tr>
id) in database?
<tr>
<th></th>
<th>Product Name</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total </th>
</tr>
// This part will append after ajax call i have used autocomplete search
<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>
Store the quantity and product in array:
<td><input type='text' name="id[]" class='productclass' value='+ui.item.id+'></td>
<td><input type='text' name="quantity[]" class='quantity' value='1'></td>
if you don't want to show the product id, you can hide:
<input type='hidden' name="id[]" class='productclass' value="+ui.item.id+">
On the server side:
//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
}
}
Try do to this. For this particularly select with reference id wise.
How About this (Just replace your ui.item with fixed value)
<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>
Using 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
}