After I append new row, select2 is not working - php

I added new row in table by jquery, then select2 is not working
// Javascript
$("#add").click(function () {
$("#mytable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() +'</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
//Html
<form method="POST" action="/store">
{{ csrf_field() }}
<h2>Invoice</h2>
<div>
<!-- Customer job-->
<div class="form-group job">
Customer job:
<select class="customer_job" name="customer_job">
#if(count($customers)>1)
#foreach($customers as $customer)
<option>{{$customer ->job}}</option>
#endforeach
#else
<p>No posts found</p>
#endif
</select>
</div>
<!--Date-->
<div class="form-group date">
Date:
<input type="date" name="date">
</div>
</div>
<br><br>
<table id="mytable">
<tr>
<th>ITEM</th>
<th>DESCRIPTION</th>
<th>QUANTITY</th>
<th>RATE</th>
<th>AMOUNT</th>
</tr>
<tr>
<td>
<select class="item" name="item">
#if(count($items)>1)
#foreach($items as $item)
<option>{{$item ->item_name}}</option>
#endforeach
#endif
</select>
</td>
<td>
<input type="text" name="description">
</td>
<td>
<input type="number" name="quantity" id="quantity" step="1">
</td>
<td>
<select class="rate" name="rate" id="rate">
#if(count($items)>1)
#foreach($items as $item)
<option>{{$item ->rate}}</option>
#endforeach
#endif
</select>
</td>
<td><input type="number" name="amount" id="amount" readonly /></td>
</tr>
</table><br>
<input type="submit" value="Save" class="submit">
<input type="button" value="Add" id="add" class="btn-primary add">
</form>

Change your js code with this code
var j =2;
$("#add").on('click', function () {
var data = '<tr><td><select class="item" name="item">#if(count($items)>1)#foreach($items as $item)<option>{{$item ->item_name}}</option> #endforeach #endif</select></td><td> <input type="text" name="description"></td> <td> <input type="number" name="quantity" id="quantity" step="1"> </td> <td> <select class="rate" name="rate" id="rate"> #if(count($items)>1) #foreach($items as $item) <option>{{$item ->rate}}</option> #endforeach #endif</select></td><td><input type="number" name="amount" id="amount" readonly /> <i class="fa fa-times-circle"></i></td></tr>';
$('#mytable tr:last').after(data);
j++;
$(".remCF").on('click', function () {
$(this).parent().parent().remove();
});
});
I hope it works

Related

DataTables warning: table id=tab_taxes_detail - Requested unknown parameter '0' for row 1, column

Laravel app where clicking on add technician will add a new row. But it's showing an error! Requested unknown parameter. This is only occurring when I used the src in response blade app as i have to use date time function there. But without that tag it isn't working and using that src is leading to this error.
This is my table in blade.php.
<div class="col-md-12 col-xs-12 col-sm-12 panel-group">
<div class="col-md-10 col-sm-8 col-xs-8">
<h3>{{ trans('Technician')}}</h3>
</div>
<div class="col-md-2 col-sm-4 col-xs-4">
<button type="button" id="add_new_tech" url="{!! url('jobcard/view/getNewTech')!!}" class="btn btn-default">Add Technician</button>
</div>
</div>
<div class="col-md-12 col-xs-12 col-sm-12">
<div class="col-md-12 col-xs-12 col-sm-12" style="margin-top:20px;">
<div class="table-responsive">
<table class="table table-bordered addTechTable" id="tab_taxes_detail" align="center">
<thead>
<tr>
<th class="actionre">{{ trans('Type')}}</th>
<th class="actionre">{{ trans('No.of Technicians')}}</th>
<th class="actionre">{{ trans('Rate/Hour')}} (<?php echo getCurrencySymbols(); ?>)</th>
<th class="actionre">{{ trans('Chargeale Hour')}}</th>
<th class="actionre">{{ trans('From Date')}}</th>
<th class="actionre">{{ trans('To Date')}}</th>
<th class="actionre">{{ trans('No Work')}}</th>
<th class="actionre">{{ trans('Work')}}</th>
<th class="actionre">{{ trans('Total Cost')}} (<?php echo getCurrencySymbols(); ?>)</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr id="row_id_1">
<td>
<select name="type[tech_id][]" class=" type select_productname_1" url="{!! url('jobcard/view/getHourRate')!!}" row_did="1" data-id="1" required="required">
#foreach ($tech as $techs)
<option value="{{$techs->id}}">{{$techs->name}}</option>
#endforeach
</select>
</td>
<td>
<input type="text" name="product[qty][]" url="{!! url('jobcard/view/getqty')!!}" class="form-control quantity qty qty_1" id="qty_1" row_id="1" style="width:100%;" value="1" maxlength="8" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')">
<span class="qty_1">
</td>
<td>
<input required type="text" name="product[price][]" class="product prices price_1" id="price_1" row_id="1" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" readonly>
</td>
<td>
<input type="number" name="chr[][]" id="chr_1" row_id="1" >
</td>
<td>
<input type="datetime-local" name="fd[][]" id="fd_1" row_id="1" class="form-control fdate fdate_1 date">
</td>
<td>
<input type="datetime-local" name="td[][]" id="td_1" row_id="1" class="form-control tdate tdate_1 date" >
</td>
<td>
<input type="number" name="nwrk[][]" id="nwrk_1" row_id="1" >
</td>
<td>
<input type="text" class="wrk_1 wrk" url="{!! url('jobcard/view/wrkdiff')!!}" name="wrk[][]" id="wrk_1" row_id="1" >
</td>
<td>
<input type="text" name="product[total_price][]" class="product total_price total_price_1" id="total_price_1" readonly>
</td>
<td align="center" hidden>
<span class="product_delete" data-id="0"><i class="fa fa-trash fa-2x"></i> </span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
This is the jQuery file.
<script>
$("#add_new_tech").click(function()
{
var row_id = $("#tab_taxes_detail > tbody > tr").length;
var url = $(this).attr('url');
$.ajax({
type: 'GET',
url: url,
data : {row_id:row_id},
beforeSend : function () {
$("#add_new_tech").prop('disabled', true);
},
success: function (response)
{
$('.addTechTable').DataTable().row.add($(response.html)).draw();
$("#add_new_tech").prop('disabled', false);
return false;
},
error: function(e) {
alert(msg100 + " " + e.responseText);
console.log(e);
}
});
});
Controller function.
<?php
public function getNewTech(Request $request)
{
$id = $request->row_id;
$ids = $id+1;
$rowid = 'row_id_'.$ids;
$tech = DB::table('tbl_tech')->where('soft_delete', '=', 0)->get();
$html = view('jobcard.newtech')->with(compact('tech','rowid','id','ids'))->render();
return response()->json(['success' => true, 'html' => $html]);
}
?>
and finally from this blade file I'm getting the response.
<script src="{{ URL::asset('vendors/jquery/dist/jquery.min.js') }}"></script>
<tr id="row_id_<?php echo $ids;?>">
<td>
<select name="type[tech_id][]" class="form-control type select_productname_<?php echo $ids;?>" row_did="<?php echo $ids;?>" url="<?php echo url('jobcard/view/getHourRate');?>" data-id="<?php echo $ids;?>" style="width:100%;" required="required">
<?php foreach($tech as $techs) { ?>
<option value="<?php echo $techs->id;?>"><?php echo $techs->name;?></option> <?php } ?>
</select>
</td>
<td>
<input type="text" name="product[qty][]" row_id="<?php echo $ids; ?>" class="quantity form-control qty qty_<?php echo $ids;?>" url="<?php echo url('jobcard/view/getqty');?>" id="qty_<?php echo $ids;?>" value="1" style="width: 100%;" maxlength="8" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')">
<span class="qty_1">
</td>
<td>
<input required type="text" name="product[price][]" class="product form-control prices price_<?php echo $ids;?>" id="price_<?php echo $ids;?>" row_id="<?php echo $ids;?>" style="width:100%;" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" readonly>
</td>
<td>
<input type="number" name="chr[][]" id="chr_<?php echo $ids;?>" style="width:100%;" row_id="<?php echo $ids;?>" style="width:100%;" >
</td>
<td>
<input type="datetime-local" name="fd[][]" id="fd_<?php echo $ids;?>" row_id="<?php echo $ids;?>" class="form-control fdate fdate_<?php echo $ids;?> date">
</td>
<td>
<input type="datetime-local"class="form-control tdate tdate_<?php echo $ids;?> date " name="td[][]" id="td_<?php echo $ids;?>" style="width:100%;" row_id="<?php echo $ids;?>" style="width:100%;" >
</td>
<td>
<input type="number" name="nwrk[][]" class="nwrk_<?php echo $ids;?>" id="nwrk_<?php echo $ids;?>" style="width:100%;" row_id="<?php echo $ids;?>" style="width:100%;" >
</td>
<td>
<input type="number" name="wrk[][]" class="wrk wrk_<?php echo $ids;?>" url="<?php echo url('jobcard/view/wrkdiff');?>" id="wrk_<?php echo $ids;?>" style="width:100%;" row_id="<?php echo $ids;?>" style="width:100%;" >
</td>
<td>
<input type="text" name="product[total_price][]" class="product form-control total_price total_price_<?php echo $ids;?>" style="width:100%;" id="total_price_<?php echo $ids;?>" readonly>
</td>
<td align="center">
<span class="product_delete tax_<?php echo $ids;?>" data-id="<?php echo $ids;?>" id="tax_<?php echo $ids;?>"><i class="fa fa-trash fa-2x"></i> </span>
</td>
</tr>
Kindly help with this, Thank you.

how to set Dynamically Add / Remove input fields in PHP with Jquery Ajax is not working

i have written this codes and it seems not working, i have attached js and html codes, i do not know what is causing it not to work at all
$(document).ready(function() {
var i = 1;
$('#add_row').click(function() {
i++;
$('#dynamic_field').append('<tr id="row' + i + '"><td> <select data-placeholder="Choose Item..." class="form-control select2 chosen-select" tabindex="2" name="itemName[]" id="item_name1" required> <option>--Select here--</option> <?php '.$items = itemView($branchId);
while ($item = mysqli_fetch_array($items)) {.
'?> <option value="<?php '.echo $item["stockId"];.
'?>"><?php '.echo $item["itemName"];.
'?></option> <?php '.
}.
' ?></select></td><td><input type="text" name="adjustedQuantity[]" id="adjusted_Quantity1" data-srno="1" class="form-control" placeholder="Quantity" /></td><td><button type="button" name="remove" id="' + i + '" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function() {
var button_id = $(this).attr("id");
$('#row' + button_id + '').remove();
});
});
<table class="table table-hover ">
<tr>
<th width="60%">Item Details</th>
<th width="30%">Quantity to Adjusted</th>
<th></th>
</tr>
<tr>
<td>
<select data-placeholder="Choose Item..." class="form-control select2 chosen-select responsive" tabindex="2" name="itemName[]" id="item_name1" required>
<option>--Select here--</option>
<?php
$items=itemView($branchId);
while ($item=mysqli_fetch_array($items)) {
?>
<option value="<?php echo $item['stockId'];?>">
<?php echo $item['itemName'];?>
</option>
<?php
}
?>
</select>
</td>
<td>
<input type="text" name="adjustedQuantity[]" id="adjusted_Quantity1" data-srno="1" class="form-control" placeholder="Quantity" />
</td>
<td>
</td>
</tr>
</table>
i have attached js and html codes, i do not know what is causing it not to work at all

Couldn't calculate drop down list select value using jquery

i am creating a simple inventory system using jquery.what is problem is when i calculation products we have a two option GR and KG. if i select as GR it need to be calculation the GR calculation part if i select as KG it need to be calculation the KG calculation part.i ran into the problem with KG is working GR not working what i tried so far i attached along with the image.
Form Design
<nav class="navbar navbar-dark bg-dark">
<span class="navbar-brand mb-0 h1">Restaurant Coffee Shop Pos</span>
</nav>
<tr>
<td>
<input type="checkbox" name="pos" value="Suger">
<label>Suger</label>
</td>
<td>
<input type="number" name="qty" id="qty" size="10px">
</td>
<td>
<select class="form-control" id="option" name="option"
placeholder="option" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Tea">
<label>Tea</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="option" name="option"
placeholder="Plan" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Flour">
<label> Flour</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="option" name="option"
placeholder="option" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Rice">
<label> Rice</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="status" name="status"
placeholder="Ricestatus" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Dhall">
<label> Dhall</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="Dhallstatus" name="status"
placeholder="status" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
</table>
<input type="button" class="btn btn-info" onclick="Add()" value="Ok" name="Ok" id="Ok">
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="container">
<div class="list-group-item list-group-tem-action active">AddProducts</div>
<table id="tbl-item" class="table table-dark table-bordered" cellspacing="0" width="100%" align="center">
<thead>
<tr>
<th>Delete</th>
<th>Item</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="col-sm-2">
<div class="list-group-item list-group-item-action active">Bill</div>
<div>
<label>Total</label>
<input type="text" style="color: yellow; background: black; font-size: 30px;" id="total" name="total" placeholder="Total" required>
</div>
</br>
<div>
<input type="button" class="btn btn-warning" value="Reset" name="reset" id="reset">
</div>
</div>
</div>
jQuery
<script type="text/javascript">
var tot = 0;
var total = 0;
function Add()
{
$("input[name='pos']:checked").each(function() {
// Get the value
let check = $(this).val();
var price = null;
var sugerstatus = $('#Sugerstatus :selected').text();
if (check == "Suger") {
price = 12;
}
else if (check == "Village Fries") {
price = 15;
} else if (check == "Fries") {
price = 35;
}
else if (check == "Tea")
{
price = 10;
}
else if (check == "Coffee")
{
price = 32;
}
else if (check == "Apple Juice")
{
price = 25;
}
else if (check == "Orange Juice")
{
price = 45;
}
var qty = $(this).closest("tr").find('[name="qty"]').val() || 0;
tot = qty * price;
$("#option").each(function()
{
if ($(this).val() == 2){
var calamount = (
qty * 140 );
$("#total").val(calamount);
}
if ($(this).val() == 1)
{
var calamount = (qty /1000 * 140 );
$("#total").val(calamount);
}
});
var table1 =
"<tr>" +
"<td><button type='button' name='record' class='btn btn-warning' onclick='deleterow(this)'>Delete </td>" +
"<td>" + check + "</td>" +
"<td>" + price + "</td>" +
"<td>" + qty + "</td>" +
"<td>" + tot + "</td>" +
"</tr>";
total += Number(tot);
$('#total').val(total);
$("#tbl-item tbody").append(table1);
});
}
function deleterow(e)
{
total_cost = parseInt($(e).parent().parent().find('td:last').text(),10);
total -= total_cost;
$('#total').val(total);
$(e).parent().parent().remove();
}
$('#reset').click(function()
{
location.reload();
}
);
</script>
Here
there GR value 1 KG value 2
$("#option").each(function()
{
if ($(this).val() == 2){
var calamount = (
qty * 140 );
$("#total").val(calamount);
}
if ($(this).val() == 1)
{
var calamount = (qty /1000 * 140 );
$("#total").val(calamount);
}
var tot = 0;
var total = 0;
function Add() {
$("input[name='pos']:checked").each(function() {
// Get the value
let check = $(this).val();
var price = null;
var sugerstatus = $('#Sugerstatus :selected').text();
if (check == "Suger") {
price = 12;
} else if (check == "Village Fries") {
price = 15;
} else if (check == "Fries") {
price = 35;
} else if (check == "Tea") {
price = 10;
} else if (check == "Coffee") {
price = 32;
} else if (check == "Apple Juice") {
price = 25;
} else if (check == "Orange Juice") {
price = 45;
}
var qty = $(this).closest("tr").find('[name="qty"]').val() || 0;
tot = qty * price;
var sel = $(this).closest("tr").find("select");
if (sel.val() == 2) {
var calamount = (
qty * 140);
$("#total").val(calamount);
console.log("val", sel.val(), calamount);
}
if (sel.val() == 1) {
var calamount = (qty / 1000 * 140);
$("#total").val(calamount);
console.log("val", sel.val(), calamount);
}
var table1 =
"<tr>" +
"<td><button type='button' name='record' class='btn btn-warning' onclick='deleterow(this)'>Delete </td>" +
"<td>" + check + "</td>" +
"<td>" + price + "</td>" +
"<td>" + qty + "</td>" +
"<td>" + tot + "</td>" +
"</tr>";
total += Number(tot);
$('#total').val(total);
$("#tbl-item tbody").append(table1);
});
}
function deleterow(e) {
total_cost = parseInt($(e).parent().parent().find('td:last').text(), 10);
total -= total_cost;
$('#total').val(total);
$(e).parent().parent().remove();
}
$('#reset').click(function() {
location.reload();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js"></script>
<nav class="navbar navbar-dark bg-dark">
<span class="navbar-brand mb-0 h1">Restaurant Coffee Shop Pos</span>
</nav>
<div class="row">
<div class="col-sm-5">
<div class="container">
<div class="list-group-item list-group-tem-action active">Item</div>
<div class="panel-body bg-dark" style="color: white">
<form id="tbl-project">
<div class="panel-body bg-dark" style="color: white">
<table class="table table-bordered" style="color: white">
<tr>
<td>
<input type="checkbox" name="pos" value="Suger">
<label>Suger</label>
</td>
<td>
<input type="number" name="qty" id="qty" size="10px">
</td>
<td>
<select class="form-control" id="option" name="option" placeholder="option" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Tea">
<label>Tea</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="option" name="option" placeholder="Plan" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Flour">
<label> Flour</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="option" name="option" placeholder="option" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Rice">
<label> Rice</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="status" name="status" placeholder="Ricestatus" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="pos" value="Dhall">
<label> Dhall</label>
</td>
<td>
<input type="number" name="qty" size="10px">
</td>
<td>
<select class="form-control" id="Dhallstatus" name="status" placeholder="status" required>
<option value="">Please Select</option>
<option value="1">GR</option>
<option value="2">KG</option>
</select>
</td>
</tr>
</table>
<input type="button" class="btn btn-info" onclick="Add()" value="Ok" name="Ok" id="Ok">
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="container">
<div class="list-group-item list-group-tem-action active">AddProducts</div>
<table id="tbl-item" class="table table-dark table-bordered" cellspacing="0" width="100%" align="center">
<thead>
<tr>
<th>Delete</th>
<th>Item</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>
<tbody>
</tbody>
</table>
</div>
</div>
<div class="col-sm-2">
<div class="list-group-item list-group-item-action active">Bill</div>
<div>
<label>Total</label>
<input type="text" style="color: yellow; background: black; font-size: 30px;" id="total" name="total" placeholder="Total" required>
</div>
</br>
<div>
<input type="button" class="btn btn-warning" value="Reset" name="reset" id="reset">
</div>
</div>
</div>
what i see its an error when you select the right select for GR or KG:
use $(this).closest("tr").find("select") instead of $("#option"), (you have to have uniq id and you have lot of id option) and no need to use loop function
var sel = $(this).closest("tr").find("select")
if (sel.val() == 2) {
var calamount = (
qty * 140);
$("#total").val(calamount);
}
if (sel.val() == 1) {
var calamount = (qty / 1000 * 140);
$("#total").val(calamount);
}
i dont figure how you calculate...

After form submitting I don't get multiple values in array

When I click Add More Button then appending the same row and when I submit the form then I getting the first value of the product_name and color_id input field.Where is the problem I didn't find that.
Here is the html code:
<table class="table table-striped table-bordered text-center table-responsive">
<thead class="text-dark">
<tr>
<th>Product Name</th>
<th>Color</th>
#foreach($size_infos as $sizee)
<th>{{$sizee->size_name}}</th>
#endforeach
<th>Add More</th>
</tr>
</thead>
<form action={{url('product-purchase-process')}}" method="POST" enctype="multipart/form-data">
#csrf
<tbody class="add_item">
<tr>
<td width="20%">
<input class="form-control input typeahead" type="text" name="product_name[]" id="product_name" placeholder="Product Name" multiple='multiple'>
</td>
<td width="14%">
<select class="form-control input" name="color_id[]">
<option value="">--Select Color--</option>
#foreach($color_infos as $color)
<option value="{{$color->color_id}}">{{$color->color_name}}</option>
#endforeach
</select>
</td>
#foreach($size_infos as $size)
<td>
<div id="inputDiv">
<input type="hidden" name="size_id[]" value="{{$size->size_id}}" multiple='multiple'>
<input style="width:70px" class="from-control" type="text" name="product_quantity[]" multiple='multiple'>
</div>
</td>
#endforeach
<td width="10%">
<button type="button" id="add_more" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i></button>
</td>
</tr>
<tr>
</tr>
</tbody>
Here is the jquery code:
<script type = "text/javascript" >
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).on('focus', "#product_name", function () {
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: "{{url('autocomplete')}}",
data: {
products: request.products
},
dataType: "json",
success: function (data) {
var resp = $.map(data, function (obj) {
// console.log(obj.product_name);
return obj.product_name;
});
response(resp);
}
});
},
minLength: 1
});
//color field and image field extend here
var htmlString = `<tr class="ree"><td width="20%"><input class="form-control input typeahead" type="text" name="product_name[]" id="product_name" placeholder="Product Name"></td><td width="14%"><select class="form-control input" name="color_id"><option value="">--Select Color--</option>#foreach($color_infos as $colorr)<option value="{{$colorr->color_id}}">{{$colorr->color_name}}</option>#endforeach</select></td>#foreach($size_infos as $sizeee)<td><div id="inputDiv"><input type="hidden" name="size_id[]" value="{{$sizeee->size_id}}"><input style="width:70px" class="from-control" type="text" name="product_quantity[]"></div></td>#endforeach<td width="10%"><button type="button" class="btn btn-danger btnRemove"><i class="fa fa-trash" aria-hidden="true"></i></button></td></tr>`;
$('#add_more').click(function () {
$('.add_item').append(htmlString); // end append
$('.ree .btnRemove').last().click(function () {
$(this).parent().last().remove();
}); // end click
}); // end click
}); </script>

send an image through ajax in on change event with different id

iam trying to update image through on change event in codeigniter
and want to update with different id each time when i select the image different. please help with the solution thanks in advance
Each time the product_image_id should change according to db table
$(document).ready(function(){
$('.images').on('change',function(){
var link = '<?php echo site_url('menu_management_con/editimage');?>';
var product_image_id = $('.id').val();
var formData= new FormData();
var file = this.files[0];
formData.append('file', file);
formData.append('product_image_id',product_image_id);
$.ajax({
type:'POST',
url: link,
data : formData,
cache: false,
processData:false,
contentType:false,
success:function(data){
alert(data);
}
});
});
});
this is my html code
am trying to update image through on change event in codeigniter
<form id="add_produc" method="post" class="addProduct" name="add_produc" action="<?=base_url()."menu/product_edit/".$data['product_id'].'/'.$data['category_id'] ?>" enctype="multipart/form-data" >
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered">
<tr>
<th class="head" colspan="2">Edit</th>
</tr>
<tr>
<td><label for="Name">Name:</label></td>
<td>
<input type="text" name="product_name" value="<?= $data['product_name']?>" id="product_name" class="form-control" placeholder="Name">
</td>
</tr>
<tr>
<td> <label for="Description">Description:</label> </td>
<td>
<input type="text" name="product_description" value="<?= $data['product_description']?>" id="product_description" class="form-control" placeholder="description">
</td>
</tr>
<tr >
<td><label for="Photo">Photo:</label> </td>
<td>
<div id="addField0">
<input type="file" name="product_photo[]" multiple id="product_photo" class="form-control" accept="image/*"><br>
</div>
<button id="add-more-edit" name="add-more-edit" class="btn btn-primary">Add More</button>
</td>
</tr>
<?php
if(!empty($image)){
foreach ($image as $result){
?>
<tr class='imagelocation<?= $result->product_image_id ?>'>
<td colspan="2" align='center'>
<input type="text" name="product_image_id" value="<?= $result->product_image_id?>" id="product_photo" class="form-control id" >
<input type="file" name="image" id="image" class="form-control images" accept="image/*">
<img class='img-rounded' height="50" width="50" src="<?= base_url('/assets/uploads/products/'.$result->product_photo)?>" style="vertical-align: middle;">
<span style="cursor:pointer;" onclick="javascript:deleteimage(<?php echo $result->product_image_id ?>)">X</span>
</td>
</tr>
<?php
}
}else{
echo '<div class="empty">No Products Found...!</div>';
}
?>
<tr>
<td colspan="2">
<input type="hidden" name="sub_category_id" value="<?= $data['sub_category_id']?>" id="sub_category_name" class="form-control">
<input type="submit" name="update_product" id="update_product" class="btn btn-info btn-small" value="Update">
</td>
</tr>
</table>
</div>
</div>
</form>

Categories