I am using ajax to updat codeigniter cart. My problem is when i add multiple items in the cart & update the quantity of items the quantity does not change. It only works for the last item added.
below is my code
<form id="columnarForm" name="columnarForm" enctype="multipart/form-data" method="post">
<table width="100%" align="center" cellpadding="0" cellspacing="0" class="table-responsive table-bordered table-hover">
<tbody>
<?php foreach($products as $product){ ?>
<tr>
<td align="right" valign="middle">
<input readonly type="text" name="name" value="<?php echo $product['name'] ?>"/>
</td>
<td align="right" valign="middle">
<input readonly type="text" name="price" value="<?php echo $product['price'] ?>"/>
</td>
<td align="right" valign="middle">
<input type="text" name="qty" value="<?php echo $product['qty'] ?>"/>
<input type="button" class="btn btn-primary btn-xs change" value="Change" />
</td>
<input type="hidden" name="rowid" value="<?php echo $product['rowid'] ?>"/>
</tr>
<script type="text/javascript">
$(function(){
$('.change').click(function(){
$.ajax({
type: "POST",
url: "<?php echo base_url()?>update-shopping-cart",
data: $("#columnarForm").serialize(),
beforeSend: function(){
$('#result').html('<img src="<?php echo site_url('assets/images/loading.gif'); ?>" class="center-block" />');
},
success: function(data){
$('#result').html(data);
}
});
});
});
</script>
<? } ?>
</tbody>
</table>
</form>
My Controller
// Updated the shopping cart
function updateCart(){
$data = array(
'rowid' => $this->input->post('rowid'),
'qty' => $this->input->post('qty'),
);
// Update the cart with the new information
$this->cart->update($data);
redirect(base_url().'update-cart-view');
}
You are looping through your products and setting all input names to be the same thing:
<td align="right" valign="middle">
<input readonly type="text" name="name" value="<?php echo $product['name'] ?>"/>
</td>
<td align="right" valign="middle">
<input readonly type="text" name="price" value="<?php echo $product['price'] ?>"/>
</td>
<td align="right" valign="middle">
<input type="text" name="qty" value="<?php echo $product['qty'] ?>"/>
So you end up with x amount of "name"s, x amount of "price"s and x amount of "qty"s. And your code grabs an element "name" with "price" with "qty". On submit, because all the inputs are in the same form, you send ALL of the information, but process only one set.
Either use the 'rowid' with every element name - 'price_rowid', 'name_rowid' and 'qty_rowid' so you can loop through all values, or loop through all post data on submit.
Related
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>
i update php form and change the value but it cant change it saves the same previous value
<tr>
<td width="22%">Course Fees</td>
<td width="38%">
<div id="total_fees">
<input type="text" class="input_text" name="total_fees" id="toal_fees" onblur="show_record(this.value,1)" value="<?php if($_POST['total_fees']) echo $_POST['total_fees']; else echo $row['total_fees'];?>" />
</div>
</td>
</tr>
Please see the screenshot of source code and input filed . i changed the value in input field but it remain same in source code and save source code value db
The input values not immediately affect on your input but You will get your new value while you submitting your form.
So submit your form and then print the values of the Request. You will find the new value of total_fees into the request parameters.
HTML CODE
<form name="jqueryForm" method="post" id="jqueryForm" enctype="multipart/form-data" onSubmit="return validme();">
<table border="0" cellspacing="15" cellpadding="0" width="100%" >
<tr>
<td width="22%">Course Fees</td>
<td width="38%">
<div id=total_fees>
<input type="text" class="input_text" name="total_fees" id="toal_fees" onblur="show_record(this.value,1)" value="<?php if($_POST['total_fees']) echo $_POST['total_fees']; else echo $row['total_fees'];?>" />
</div>
</td>
<td width="40%"></td>
</tr>
<tr>
<td width="22%">Discount in <input type="radio" name='discount_type' value="<?php if($_POST['discount_type']) echo $_POST['discount_type']; else echo percent; ?>" checked="checked" onChange="show_record()" />% or <input type="radio" name='discount_type' value="<?php if($_POST['discount_type']) echo $_POST['discount_type']; else echo cash; ?>" onChange="show_record()" />Cash</td>
<td width="38%"><input type="text" class="input_text" name="concession" id="concession" value="<?php if($_POST['concession']) echo $_POST['concession']; else if($row_record['discount'] !=0) echo $row_record['discount']; else echo 0; ?>" onBlur="show_record()"/>
</td>
<td width="40%"></td>
</tr>
</table>
<input type="submit" value="Save Record" name="save_changes" />
</form>
PHP CODE FOR SAVE IN DB
if(isset($_POST['save_changes']))
{
$total_fees=($_POST['total_fees']) ? $_POST['total_fees'] : "0";
$data_record['total_fees']=$total_fees;
$courses_id=$db->query_insert("enroll", $data_record);
}
I'm trying to send text from my <td> with my simple ajax to my controller (I'm using Codeigniter).
Here is my HTML
<table class="table table-striped col-md-9 cstm">
<thead>
<tr>
<td> <strong>Transaction No</strong> </td>
<td> <strong> Line No </strong> </td>
<td> <strong>Item Code</strong> </td>
<td> <strong>Item Name</strong> </td>
<td> <strong>Extra Remark</strong> </td>
<td> <strong>Extra Remark 2</strong> </td>
<td width="10%"> <strong>Item QTY</strong> </td>
<td width="10%"> <strong>Receive QTY</strong> </td>
<td width="10%"> <strong>UOM Code</strong> </td>
<td width="10%"> <strong>Extra Remark Receipt</strong> </td>
</tr>
</thead>
<tbody>
<tr>
<td class="notrans">DORD-DK-M-TAM-69</td>
<td>1000 <input value="1000" type="hidden" name="LineNo[]" /> </td>
<td>S160291 <input value="S160291" type="hidden" name="ItemCode[]" /> </td>
<td>SMALL BAG <input value="SMALL BAG" type="hidden" name="ItemName[]" /> </td>
<td> <input value="" type="hidden" name="ExtraRemark[]" /></td>
<td> <input value="" type="hidden" name="ExtraRemark2[]" /></td>
<td>2 <input value="2.00000000000000000000" type="hidden" name="Quantity[]" /> </td>
<td><input required="required" type="text" value="2" name="recv_qty[]" /> </td>
<td> PCS<input type="hidden" value="PCS" name="uom[]" /> </td>
<td><input type="text" value="" name="ext_rmrk[]" /></td>
</tr>
<tr>
<td class="notrans">DORD-DK-M-TAM-70</td>
<td>2000 <input value="2000" type="hidden" name="LineNo[]" /> </td>
<td>S160288 <input value="S160288" type="hidden" name="ItemCode[]" /> </td>
<td>SMALL BAG <input value="SMALL BAG" type="hidden" name="ItemName[]" /> </td>
<td> <input value="" type="hidden" name="ExtraRemark[]" /></td>
<td> <input value="" type="hidden" name="ExtraRemark2[]" /></td>
<td>2 <input value="2.00000000000000000000" type="hidden" name="Quantity[]" /> </td>
<td><input required="required" type="text" value="2" name="recv_qty[]" /> </td>
<td> PCS<input type="hidden" value="PCS" name="uom[]" /> </td>
<td><input type="text" value="" name="ext_rmrk[]" /></td>
</tr>
</tbody>
</table>
<a class="pull-right btn btn-warning" id="update" href="#"> Update </a>
and here is my Jquery
$("#update").click(function(){
$this = $(this).val()
$.ajax({
data : { DetTrans : $('.notrans').text() },
url : "<?=base_url();?>dr_mutasi/updateEditItem",
type : "POST",
dataType: 'JSON',
success: function(data) {
console.log(data);
}
})
});
and here is my PHP
function updateEditItem(){
extract(populateform());
echo $DetTrans;
}
So, With my script abobve . I get this from my console.log
DORD-DK-M-TAM-69DORD-DK-M-TAM-70
As you can see, from my table there are .notrans and both of them has different value. Can you tell me how to send via ajax with this format ?
DORD-DK-M-TAM-69;DORD-DK-M-TAM-70
so i can explode it later with my PHP. Thanks in advance.
$("#update").click(function(){var value="";
$(document).find('.notrans').each(function(){
value += (value.length > 0) ? ';' + $(this).text():$(this).text();
});
$.ajax({
data : { DetTrans : value},
url : "<?=base_url();?>dr_mutasi/updateEditItem",
type : "POST",
dataType: 'JSON',
success: function(data) {
console.log(data);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-striped col-md-9 cstm">
<thead>
<tr><td> <strong>Transaction No</strong> </td> </tr>
</thead>
<tbody>
<tr><td class="notrans">DORD-DK-M-TAM-69</td></tr>
<tr><td class="notrans">DORD-DK-M-TAM-70</td></tr>
<tr><td class="notrans">DORD-DK-M-TAM-71</td></tr>
<tr><td class="notrans">DORD-DK-M-TAM-72</td></tr>
</tbody>
</table>
<a class="pull-right btn btn-warning" id="update" href="#"> Update </a>
You can update your jquery to get the value of all td containing notrans class.
<script type="text/javascript">
$(function(){
var value = '';
$('.notrans').each(function(){
if(value.length > 0)
value = value + ';' + $(this).text(); // will give you the value.
else
value = $(this).text();
});
});
</script>
Use value to send the data through ajax.
I have nine text boxes wrapped in a form but when post to codeigniter controller, there are two specific text boxes have no value but they actually have.
Anyone encountered this issue before ? what is actually wrong ?
Form
<form name="frm_RRequest" id="frm_RRequest" action="<?php echo site_url('user/add_recommendation_request/'); ?>" method="post">
<tbody>
<tr>
<td class="col-left">Date</td>
<td class="col-middle"><input class="datepicker" type="text" name="txtStartDate" id="txtStartDate" class="datepicker" placeholder="Click to select a start date.."></td>
<td class="col-middle"><input class="datepicker" type="text" name="txtEndDate" id="txtEndDate" class="datepicker" placeholder="Click to select a end date.."></td>
<td class="col-right"><div class="error" id="error_date"> </div></td>
</tr>
<tr>
<td class="col-left">Travel time</td>
<td class="col-middle"><input type="text" class="ptTimeSelect input" name="txtStartTime" id="txtStartTime" placeholder="Click to select start time.." data-default-time="false"></td>
<td class="col-middle"><input type="text" class="ptTimeSelect input" name="txtEndTime" id="txtEndTime" placeholder="Click to select end time.." data-default-time="false"></td>
<td class="col-right"><div class="error" id="error_time"> </div></td>
</tr>
<tr>
<td class="col-left">Location</td>
<td class="col-middle-2"><input type="text" class="inputWithImge" name="txtStartLocation" id="txtStartLocation" onmouseover="display_text(this)" placeholder="Click the icon to select a start point"/><img src="<?php echo base_url('assets/images/search_icon.png'); ?>" class="location-icon" title="Click to show map" name="location-icon_start" value="StartLocation"/></td>
<td class="col-middle-2"><input type="text" class="inputWithImge" name="txtEndLocation" id="txtEndLocation" onmouseover="display_text(this)" placeholder="Click the icon to select a end point"/><img src="<?php echo base_url('assets/images/search_icon.png'); ?>" class="location-icon" title="Click to show map" name="location-icon_end" value="EndLocation" /></td>
<td class="col-right"><div class="error" id="error_location"> </div></td>
</tr>
<input type="hidden" name="txtStartLocation_Coordinates" id="txtStartLocation_Coordinates">
<input type="hidden" name="txtEndLocation_Coordinates" id="txtEndLocation_Coordinates">
</tbody>
</table>
</div>
<div><input type="button" class="button" id="btnGo" name="btnGo" value="Input detail" /> <span> << click this button if the travel time and location(s) are different for each day</span></div>
<div id="detail">
</div>
<input type="hidden" name="txtTotalDetail" id="txtTotalDetail">
<input type="hidden" name="txtNoOfDays" id="txtNoOfDays">
<div> </div>
<div><input type="button" id="btn_SaveDetail" name="btn_SaveDetail" class="button" value="Save" /></div>
</form>
<script>
function display_text(obj){
var value = obj.value;
obj.title = value;
}
</script>
Controller :
$total_detail = $this->input->post("txtTotalDetail");
$noOfDays = $this->input->post("txtNoOfDays");
$userid = $this->session->userdata('id');
$start_date = $this->input->post("txtStartDate");
$end_date = $this->input->post("txtEndDate");
$start_time = $this->input->post("txtStartTime");
$end_time = $this->input->post("txtEndTime");
$start_location = $this->input->post("txtStartLocation");
$end_location = $this->input->post("txtEndLocation");
$start_location_coor = $this->input->post("txtStartLocation_Coordinates");
$end_location_coor = $this->input->post("txtEndLocation_Coordinates");
These two text boxes have no value :
$start_location = $this->input->post("txtStartLocation");
$end_location = $this->input->post("txtEndLocation");
It is possible you forgot to use $this->form_validation->set_rules() on those particular fields.
Put value in your input tag like
<input type="hidden" name="txtStartLocation_Coordinates" value ="<?php echo $this->input->post('txtStartLocation_Coordinates');?>" id="txtStartLocation_Coordinates">
<input type="hidden" name="txtEndLocation_Coordinates" value ="<?php echo $this->input->post('txtEndLocation_Coordinates');?>" id="txtEndLocation_Coordinates">
<script>
$(function(){
$('#txtStartLocation_Coordinates').val("your value");
$('#txtEndLocation_Coordinates').val("your value");
});
</script>
I'm trying to build an application using php. in "stock_out.php", i wanna create a form that include two barcodes. I have 8 fields. (1)Effectivedate,(2)Partnumber,(3)Description1,(4)Description2,(5)NPK,(6)Nama,(7)QtyOut,(8)Reason. I wanna using barcode-reader in "Partnumber" and "NPK".
I have some problems:
1. When i press enter, it's respon for button submit.
2. Actually, when i press enter in Partnumber, i want show details data in "description1" and "description2". As well as in NPK, i want show details data in "nama"
3. In the end of form, i still want to submit this form into database.
I'm newbie in AJAX, so i still don't know how to implementation AJAX in PHP. This is my code, I am grateful for the help
<html><body>
<div id="outerContainer">
<?php include("headerFile.html"); ?>
<?php include("navigationFile.html"); ?>
<div id="bodyContainer">
<div id="pageBodyTitle"> Stock Out </div> <br>
<form id="formSearch" name="formSearch" method="post" action="proses_out.php" onSubmit="return cek_data();">
<table id="messageTable">
<tr>
<td class="heading"> Effective Date </td>
<td>
<input class="inputField" name="tgl" type="text" readonly value="<?php echo $_POST['tgl']?>" tabindex="1"/>
<script language='JavaScript'>new tcal ({'formname': 'formSearch','controlname': 'tgl'});</script>
</td>
</tr>
<tr>
<td class="heading"> Part Number </td>
<td><input type='text' name='txtItem' id='txtItem' size="20" class="inputField" value='<?php echo $item;?>' tabindex="2" />
<img src='search.ico' onClick='open_win_item()'> </td>
</tr>
<tr>
<td class="heading">Description1</td>
<td><input type='text' name='txtDesc1' id='txtDesc1' size="50" value='<?php echo $desc1;?>' readonly /></td>
</tr>
<tr>
<td class="heading">Description2</td>
<td><input type='text' name='txtDesc2' id='txtDesc2' size="50" value='<?php echo $desc2;?>' readonly /></td>
</tr>
<tr>
<td class="heading"> NPK </td>
<td><input type='text' name='txtNpk' id='txtNpk' size="20" maxlength="5" class="inputField" value='<?php echo $tr_no;?>' tabindex="3" />
<img src='search.ico' onClick='open_win_npk()'></td>
</tr>
<tr>
<td class="heading">Nama</td>
<td><input type='text' name='txtName' id='txtName' size="30" value='<?php echo $nama;?>' readonly /></td>
</tr>
<tr>
<td class="heading"> Qty Out </td>
<td><input type='text' name='txtQty' id='txtQty' size="5" class="inputField" tabindex="4"/></td>
</tr>
<tr>
<td class="heading"> Reason </td>
<td><select class="inputField" name="choose" id="choose" value="" tabindex="5">
<?php
include "/class/connect_SQL.class.php";
$sql = "select reason_code from Inv_reason_master";
$query = mssql_query($sql);
while ($row = mssql_fetch_array($query))
{
$coba = $row['reason_code'];
?>
<option value="<?php echo $coba; ?>"><?php echo $coba;?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="Submit" value="Save" class="formButton2" tabindex="6"/>
<input type="reset" name="reset" value="Cancel" class="formButton2" tabindex="7"/>
</td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_GET["message"]) and $_GET["message"]='save')
{
echo "<script language=\"javascript\"> alert('Data Tersimpan!') </script>";
}
?>
<?php include("footerFile.html"); ?>
</div>
You will have to prevent the response of the "Enter"-key. If you are using jquery in your project you can prevent it with this code:
$(function() {
$("form").bind("keypress", function(e) {
if (e.keyCode == 13) return false;
});
});
To bind Enter key to "Part number" field instead you could try this:
$('#txtItem').keypress(function(e) {
if (e.keyCode == 13) {
alert('Enter is pressed in part number');
}
});