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

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>

Related

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

jquery-select for additional rows

I am trying to use jquery-select2 in my select dropdown form. The form is in a table row and I can add new rows with the Add Button. When I click the Add Button it will make more rows for me and I have data that is generated from the database.
The problem is that the jquery-select2 only works for the select dropdown in the first row, but it does not function for the new rows I create. I have tried the $('#idone').select2(); , but it didn’t work. Below is my select form and code.
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<div class="container">
<div class="row">
<form class="col-md-12">
<table class="table table-bordered" width="100%" id="dataTable" cellspacing="0">
<tr>
<td colspan="4" align="left"><button id="alertred" type="button" class="btn btn-sm btn-danger btn-flat pull-left delete_row" >Delete Row</button></td>
<td colspan="3" align="right"><button id="button2" type="button" class="btn btn-sm btn-success btn-flat pull-right add_row"> Add Row</button></td>
</tr>
<tr>
<td ></td>
<td>Dropdown One</td>
<td>Dropdown Two</td>
<td>details</td>
<td>Date</td>
</tr>
<tbody>
<tr valign="baseline" id="main_tr" class="main_tr" >
<td><input type="checkbox" name="deleterow" class="deleterow" ></td>
<td><select name="idone[]" class="onesub form-control" id="idone" required>
<option value="0">Select Account</option>
<option value="1">Laravel</option>
<option value="2">Jquery</option>
<option value="3">PHP</option>
<option value="4">React</option>
<option value="5">Jquery ui</option>
<option value="6">Android</option>
</select>
<select name="subone[]" class="suboneid" type="hidden"><option></option></select>
</td>
<td><select name="idtwo[]" class="twosub form-control" id="idtwo" required>
<option value="0">Select Account</option>
<option value="1">Laravel</option>
<option value="2">Jquery</option>
<option value="3">PHP</option>
<option value="4">React</option>
<option value="5">Jquery ui</option>
<option value="6">Android</option>
</select>
</td>
<td><input type="text" name="details[]" id="details" class="form-control"></td>
<td><input type="date" name="dateofentry[]" class="form-control"></td>
</tr>
<tr >
<td colspan="7" align="center"><input type="submit" id="submit" class="btn btn-info" value="Submit"></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
Below is the script to add more rows
<script type="text/javascript">
$(function() {
$(".add_row").click(function(){
var main_tr = $('#main_tr').clone();
var ml = $(".main_tr").length;
main_tr.find("label").remove();
main_tr.find("input").val("");
main_tr.find("select").val("");
main_tr.find("#idone").attr("id","idone"+ml );
main_tr.find("#idtwo").attr("id","idtwo"+ml );
main_tr.find("#details").attr("id","details"+ml );
main_tr.find("#date").attr("id","date"+ml );
$(main_tr).insertAfter('.main_tr:last');
});
$(".delete_row").click(function(){
$(".deleterow:checked").each(function () {
$(this).parents('.main_tr').remove();
});
});
});
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
});
});
$(function() {
$('#idone').select2();
$('#idtwo').select2();
});
$(document).ready(function()
{
$("body").on("change",".onesub",function(){
var headid=$(this).val();
var that = $(this);
var post_id = 'headid='+ headid;
$.ajax
({
type: "POST",
url: "ajaxpostone.php",
data: post_id,
cache: false,
success: function(suboneid)
{
$(that).parents("tr").find(".suboneid").html(suboneid);
}
});
});
});
</script>
How do I make this jquery-select2 work for the select dropdown in first row and every other new row I add to my form table.

After I append new row, select2 is not working

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

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>

PHP empty field after ajax replace

I have form with few inputs and two selects. When I select brand i want to see model options. Everything is working. But when i click submit, vehicle_model field is empty. Fields in $_POST are filled except vehicle_model field. I don't know why.
<div class="form-group">
<select class="form-control brand-select" name="brand" required="">
<option selected="true" value="1">MAN</option>
<option value="2">Volvo</option>
<option value="3">Renault</option>
<option value="4">DAF</option>
</select>
</div>
<div class="form-group" id="ajax_model">
<select class="form-control" name="vehicle_model" required="">
<option selected="" value="1">XS1</option>
<option value="2">XS2</option>
</select>
</div>
$(".brand-select").change(function() {
var id_brand = $(this).val();
var vehicle_type = <?php echo $type;?>;
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "vehicle/ajax_model",
data: {
'id_brand': id_brand,
'vehicle_type': vehicle_type
},
dataType: 'text',
success: function(data) {
$("#ajax_model").html(data);
}
});
});
My full code for form. AJAX is working - I get models after selected brand. But when I click "save" PHP doesn't see $vehicle_model. But only when i run AJAX. When I refresh page and don't change brand, everything is working.
<table id="tabela" class="table table-bordered table-striped">
<tbody>
<form action="http://192.168.0.200:9000/vehicle/update_information" method="post" accept-charset="utf-8">
<tr>
<td>Number:</td>
<td>
<div class="form-group"><input type="text" class="form-control" autocomplete="off" value="12345" name="number"></div>
</td>
</tr>
<tr>
<td>VIN:</td>
<td>
<div class="form-group"><input type="text" class="form-control" autocomplete="off" maxlength="17" minlength="17" value="12345678901234567" name="vin"></div>
</td>
</tr>
<tr>
<td>Brand:</td>
<td>
<div class="form-group">
<select class="form-control brand-select" name="brand" required>
<option selected="true" value='1'>MAN</option>
<option value='2'>Volvo</option>
<option value='3'>Renault</option>
<option value='4'>DAF</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Model:</td>
<td>
<div class="form-group" id="ajax_model">
<select class="form-control" name="vehicle_model" required>
<option selected value="1">XS1</option>
<option value="2">XS2</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Kilometers</td>
<td>
<div class="form-group"><input type="text" class="form-control" value="100 km" disabled></div>
</td>
</tr>
<tr>
<td>Comment:</td>
<td>
<textarea class="form-control" rows="5" name="comment" id="comment"></textarea>
<input type="hidden" name="id_vehicle" value="137" />
</td>
</tr>
<tr>
<td colspan="2"><button type='submit' class='btn btn-md btn-success' style="float:right;margin-top:10px;width:80px;">Save</button>
</td>
</tr>
</form>
</tbody>
</table>
Because you forgot to send vehicle_model
just try like this
<script>
$( ".brand-select" ).change(function() {
var id_brand = $(this).val();
var vehicle_model=$("select[name=vehicle_model]").val()
var vehicle_type = <?php echo $type;?>;
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "vehicle/ajax_model",
data: {
'id_brand': id_brand,
'vehicle_type': vehicle_type,
'vehicle_model':vehicle_model},
dataType: 'text',
success: function(data){
$( "#ajax_model" ).html( data );
}
});
});
</script>

Categories