Values from textbox array not updating - php

I'm new in this forum and I'm having a hard time updating selected value from checkbox coming from the textbox value.
Scenario:
I want to update selected items, For example, I want to update Item-2 and the textbox from item received will be enabled, I will enter a number for example 1 and the textbox from total receive will automatically sum using AJAX.
The problem is after submitting the value from total receive the records that were updating is blank, but when I try to check and print_r the value is there.
And one more thing if all checkboxes are checked and enter a number for each item received, the value that it only get is the last value and will be updated will all selected checkbox.
Notes:
The checkbox is an array
Textbox from total receive is an array
Here's my UI:
Controller:
public function recitem_insert(){
$this->load->model('dbquery');
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
if(isset($check)){ //Check if check is checked
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
echo "<script type='text/javascript'>
alert('Successfully Added!');
window.close();
</script>";
}else{ //End If
echo 'Please select a checkbox';
}
}
View:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Model:
public function modUpdatedynamicval($table, $column, $data, $equal_to){
$this->db->where($column, $equal_to);
$this->db->update($table, $data);
}
Edit
Let's assume:
I've input 1 in item_receive textbox one and the total receive will be 10,
2 in item_receive textbox two and the total receive will be 11,
3 in item_receive textbox three and the total receive will be 12,
CODE:
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
echo 'Check Value';
print_r($check);
echo '<br><br>';
echo 'Total Recieve';
print_r($total_rec);
OUTPUT:
Check ValueArray ( [0] => 1 [1] => 2 [2] => 3 )
Total RecieveArray ( [0] => 10 [1] => 11 [2] => 12 )
But If I only input the second textbox here's the output:
Check ValueArray ( [0] => 2 )
Total RecieveArray ( [0] => [1] => 11 [2] => )

From what I see, you are having a problem to determine which input index is which on the back end. You could set an index key on each of the input like this :
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $key => $row){ //added key to be used as inputs key
?>
<tr>
<td><input type="checkbox" name="check[<?php echo $key ?>]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $key ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $key ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $key ?>]"></td>
</tr>
<?php
}
?>
</tbody>
After that, then replace the for loop :
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
With this foreach :
foreach ($check as $key => $item) {
$updateData = array('rec_qty' => $total_rec[$key] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$key]);
}
So you don't manually check for the total_rec value but instead it only checks on submitted check post data.

from what i can see, like everybody else is saying, you have trouble in finding the correct rows of checked item.
I suggest you edit the way you name the form in multidimensional array. bet you didnt know that?
so in view.php
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//WTF?? it's 2019. Say hello to SQL Injection
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $i=>$row){
?>
<tr>
<td><input type="checkbox" name="data[<?php echo $i;?>][check]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="data[<?php echo $i;?>][last_item_rec]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="data[<?php echo $i;?>][item_rec]" id="txt" disabled=""></td>
<td><input type="text" name="data[<?php echo $i;?>][total_rec]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
and you can easily cycle through the data for each rows while at the same time validating if the checkbox is checked
<?php
//simple way of traversing the data
if ( isset( $_POST['data'] ) )
{
echo '<table>';
foreach ( $_POST['data'] as $d )
{
//you have all the data here
//start checking on checked box
if(isset($d['check']))
{
echo '<tr>';
echo ' <td>', $d['last_item_rec'], '</td>';
echo ' <td>', $d['item_rec'], '</td>';
echo ' <td>', $d['total_rec'], '</td>';
echo '</tr>';
}
}
echo '</table>';
}

Can try with row key (item id). Update your array key with $row->id.
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $row->id; ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $row->id; ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $row->id; ?>]"></td>
</tr>
<?php
}
?>
</tbody>
Scenraio Like:
1 item Id "2"
2 item Id "3"
3 item Id "5"
If you check first st row and first row item_id ($row->id) is 2 and enter 7 than you will get array like :-
print_r($_POST['total_rec']);
Output Like:
Array([2]=>7)
You can easy to find data using array key
Update:
<form method="post" action="<?php echo base_url(); ?>">
<div class="box">
<div class="box-header">
<h3 class="box-title">
System ID: <b> <?php echo '12'; ?></b>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th> Action</th>
<th>Item Code </th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
$new_item_list = array(
array('id'=> '1','item_code'=> 'ITEM-1','description' => 'ITEM Desc', 'qty_order'=> '2','rec_qty'=> '2'),
array('id'=> '2','item_code'=> 'ITEM-2','description' => 'ITEM Desc 2', 'qty_order'=> '3','rec_qty'=> '3' ),
array('id'=> '3','item_code'=> 'ITEM-3','description' => 'ITEM Desc', 'qty_order'=> '5','rec_qty'=> '4' ),
array('id'=> '4','item_code'=> 'ITEM-4','description' => 'ITEM Desc', 'qty_order'=> '1','rec_qty'=> '3' )
);
foreach ($new_item_list as $row){
?>
<tr>
<td><input type="checkbox" name="<?php echo $row['id']; ?>[check]" id="opt" value="<?php echo $row['id']; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['qty_order']; ?></td>
<td><?php echo $row['rec_qty']; ?></td>
<input type="hidden" name="<?php echo $row['id']; ?>[last_item_rec]" value="<?php echo $row['rec_qty']; ?>">
<td><input type="text" name="<?php echo $row['id']; ?>[item_rec]" id="txt" value='' ></td>
<td><input type="text" name="<?php echo $row['id']; ?>[total_rec]" value=''>
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Submit Action: For example
foreach($_POST as $key => $value){
if(isset($value['check'])){
//Row ID of item table $key
$total = $value['total_rec'] * $value['item_rec'];
echo "Total Number :".$total."</br>";
#here is your update query
}
}
Link:- https://prnt.sc/mswdoc
If check checkbook and submit from then you will get output like
Output:- https://prnt.sc/mswefv

Your checkboxes all have the same ID, which always leads to problems. Change the id of the checkbox in the loop.
<td><input type="checkbox" name="check[]" id="opt-<?php echo $row->id; ?>" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>

Related

PHP: Show <td> elements if certain criteria is true

Basically I need to have the columns with my delete and edit buttons visible only if the admin logs in, I know how to determine if an admin is logged in. Is there a way to echo the tags that need to be shown? Thanks in advance!
<table>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Status</th>
<th>Edit?</th>
<th>Delete?</th>
</tr>
<?php foreach ($users as $user) : ?>
<tr>
<td><?php echo $user->getUser_id();?></td>
<td><?php echo $user->getFirstName().' '.$user->getLastName();?></td>
<td><?php echo $user->getEmailAddress(); ?></td>
<td><?php echo $user->getAddress();?></td>
<td><?php echo $user->getCity(); ?></td>
<td><?php echo $user->getState(); ?></td>
<td><?php echo $user->getZipCode(); ?></td>
<td><?php echo $user->getActiveDescription();?></td>
these two <td> should only be shown if x is true:
<td>
<form action ="" method="post">
<input type="hidden" name="controllerRequest"
value="show_user_edit">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Edit"></form>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="controllerRequest"
value="delete_user">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Delete" id="red">
</form>
</td>"
;
}
}
?>
</tr>
<?php endforeach; ?>
</table>
You need to use <?php if(x): ?> and <?php endif ?>see: https://www.php.net/manual/en/control-structures.if.php#112231

Can't delete without getting "Undefined Offset" error

I'm doing a very simple project for my assignment and it's about shopping cart. I've created a cart page by using SESSION with array and index to keep track user's order. The problem is whenever I'm deleting a meal from my cart list, there'll be an error which is "Notice: Undefined offset: 1", "Notice: Undefined offset: 2", etc. I'm attaching my codes here:
This is my cart page. The error said it has something to do on the line that I have commented:
<form method="POST" action="save_cart.php">
<table class="table">
<tbody>
<thead>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</thead>
<?php
//initialize total
$total = 0;
if(!empty($_SESSION['cart'])){
//create array of initail qty which is 1
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM meal WHERE meal_id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
?>
<tr>
<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>
//THE ERROR IS HERE
<td>RM <?php echo number_format($_SESSION['qty_array'][$index]*$row['meal_price'], 2); ?></td>
<?php $total += $_SESSION['qty_array'][$index]*$row['meal_price']; ?>
</tr>
<?php
$index ++;
}
}
else{
?>
<tr>
<td colspan="5" class="text-center" style="text-align: center;">No Meal in Cart</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="left"><b>Total</b></td>
<td><b>RM <?php echo number_format($total, 2); ?></b></td>
</tr>
</tr>
Here's my delete page:
session_start();
//remove the id from our cart array
$key = array_search($_GET['meal_id'], $_SESSION['cart']);
unset($_SESSION['cart'][$key]);
unset($_SESSION['qty_array'][$_GET['index']]);
//rearrange array after unset
$_SESSION['qty_array'] = array_values($_SESSION['qty_array']);
$_SESSION['message'] = "<script>alert('Meal is deleted from your cart');</script>";
header('location: cart.php');
I really need your help. Thanks in advance!
Its an index error because index doesn't exists in the array replace your html php code inside while loop with this one.
<tr>
<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0); ?>" name="qty_<?php echo $index; ?>"></td>
//THE ERROR IS HERE
<td>RM <?php
$qty = (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0);
echo number_format($qty *$row['meal_price'], 2); ?></td>
<?php $total += $qty*$row['meal_price']; ?>
</tr>

How to get multiple selected checkbox values

I am workin on PHP codeigniter
See this attached image, here i need to get values of selected checkboxes and save in a database table by clicking on the button called CONFIRM
My view Code:
<?php foreach($studentlist as $llist){
echo form_open("trusts/MoveData?id=".$llist['id']); }?>
<button name="submit" type="submit" value="<?php echo #$studentlist1->id; ?>" class="btn btn-success btn-flat pull-right marginBot10px"><i class="icon_set_1_icon-76"></i> CONFIRM </button>
<?php echo form_close(); ?>
<table class="table table-bordered" id="employee_grid">
<thead>
<tr>
<!--<th><input type="checkbox" id="select_all"></th>-->
<th>Select</th>
<th>Student Name</th>
<th>Gender</th>
<th>Email</th>
<th>Phone</th>
<th>Program Id</th>
<th>Program Name</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($studentlist as $llist){
?>
<tr id="<?php echo $llist["id"]; ?>">
<td><input type="checkbox" name="ids[]" value="<?php echo $llist['id']; ?>"> </td>
<td><?php echo $llist['Name']; ?></td>
<td><?php echo $llist['Gender']; ?></td>
<td><?php echo $llist['Email']; ?></td>
<td><?php echo $llist['Phone']; ?></td>
<td><?php echo $llist['ProgramId']; ?></td>
<td><?php echo $llist['ProgramName']; ?></td>
<!-- <td></td>-->
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
How get selected checkbox values of this view in my controller?
When the form is submitted you an get the selected checkbox values like:
$selected = $this->input->post('ids');
here $selected is an array, so use foreach() to get its values.

Value inserting on last row

Shown in the image above, I have two entries on my stock_transfer table.
As an example, I will insert two different values to show you that it's working. (5 stocks on Product 1 and 10 stocks on Product 2)
Now, when I try to insert the stocks requested remaining on Product 1 and leave the Product 2, it will be successful.
Here comes the problem. When I try to insert value (290 stocks) on my Product 2, it will insert the value in the Product 1 table.
Can anyone help me with this? Here's my query code:
if (isset($_POST['addCart']) && $_POST['addCart']=="Confirm Stock Transfer") {
foreach($_POST['qtyBuy'] as $index=>$value){
if($value > 0){
$cartProd_id = $_POST['product_id'][$index];
$cartProd_name = $_POST['product_name'][$index];
$cartProd_date = $_POST['product_exp'][$index];
$cartProd_desc = $_POST['product_desc'][$index];
$cartProd_transid = $_POST['transfer_id'][$index];
//Update stock_transfer requests
$update_stock = "UPDATE stock_transfer SET stocks_transferred = stocks_transferred + $value WHERE transfer_id = $cartProd_transid;";
$update_stockE = mysqli_query($connection, $update_stock);
Here's is my code:
<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Description</th>
<th>Sell Price</th>
<th>Expiry Date</th>
<th>Instock</th>
<th>Stocks Requested Remaining</th>
<th>Stocks Transferred</th>
<th>Quantity to Transfer</th>
</tr>
</thead>
<tbody>
<?php
$getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
$getReqE = mysqli_query($connection, $getReq);
while ($row = mysqli_fetch_array($getReqE)) {
$transfer_id = $row['transfer_id'];
$transfer_number = $row['transfer_number'];
$transfer_status = $row['transfer_status'];
$transfer_frombranch = $row['transfer_frombranch'];
$transfer_tobranch = $row['transfer_tobranch'];
$transfer_product_id = $row['transfer_product_id'];
$transfer_quantity = $row['transfer_quantity'];
$transfer_date = $row['transfer_date'];
$stocks_transferred = $row['stocks_transferred'];
$remainingStocks = $transfer_quantity - $stocks_transferred;
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_price = $row['sell_price'];
$description = $row['description'];
$quantity = $row['quantity'];
$expiry_date = $row['expiry_date'];
echo $transfer_id;
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
<input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
</td>
<input type="hidden" name="transfer_id[]" value="<?php echo $transfer_id; ?>">
<td><?php echo $product_name; ?>
<input type="hidden" name="product_name[]" value="<?php echo $product_name; ?>">
</td>
<td><?php echo $description; ?>
<input type="hidden" name="product_desc[]" value="<?php echo $description; ?>">
</td>
<td>₱ <?php echo number_format($product_price, 2); ?></td>
<td><?php echo $expiry_date; ?>
<input type="hidden" name="product_exp[]" value="<?php echo $expiry_date; ?>">
</td>
<td><strong><?php echo $quantity; ?></strong></td>
<td><?php echo $remainingStocks; ?></td>
<td><?php echo $stocks_transferred; ?></td>
<td>
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
Back to Request List
</div>
Tags
Here's my schema:
See this:
<td>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>" <?php echo ($remainingStocks == 0') ? 'Disabled=true' : '';?> value="0">
</td>
for security issues, I advise you to use ajax and submit just the qty input for one row each time:
<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Description</th>
<th>Sell Price</th>
<th>Expiry Date</th>
<th>Instock</th>
<th>Stocks Requested Remaining</th>
<th>Stocks Transferred</th>
<th>Quantity to Transfer</th>
</tr>
</thead>
<tbody>
<?php
$getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
$getReqE = mysqli_query($connection, $getReq);
while ($row = mysqli_fetch_array($getReqE)) {
$transfer_id = $row['transfer_id'];
$transfer_number = $row['transfer_number'];
$transfer_status = $row['transfer_status'];
$transfer_frombranch = $row['transfer_frombranch'];
$transfer_tobranch = $row['transfer_tobranch'];
$transfer_product_id = $row['transfer_product_id'];
$transfer_quantity = $row['transfer_quantity'];
$transfer_date = $row['transfer_date'];
$stocks_transferred = $row['stocks_transferred'];
$remainingStocks = $transfer_quantity - $stocks_transferred;
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_price = $row['sell_price'];
$description = $row['description'];
$quantity = $row['quantity'];
$expiry_date = $row['expiry_date'];
echo $transfer_id;
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
</td>
<td><?php echo $product_name; ?>
</td>
<td><?php echo $description; ?>
</td>
<td>₱ <?php echo number_format($product_price, 2); ?></td>
<td><?php echo $expiry_date; ?>
</td>
<td><strong><?php echo $quantity; ?></strong></td>
<td><?php echo $remainingStocks; ?></td>
<td><?php echo $stocks_transferred; ?></td>
<td>
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="qty_buy_<?= $transfer_id ?>" min="1" max="<?php echo $remainingStocks;?>"><a class="btn btn-sm add_qty" data-id="<?= $transfer_id ?>"></a>
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
Back to Request List
</div>
<script>
$(function() {
$('.add_qty').click(function() {
var id = $(this).data('id');
var qty = $("#qty_buy_"+id).val();
$.ajax({
type: 'GET',
url:'/your_action',
data: {id : id, qty : qty},
dataType: 'JSON',
}).done(function(json){
// code to update the values in your row
});
});
});
</script>
The problem was on the
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
I think the problem is with your input names. You have assigned arrays as names of your inputs. Try to make them unique using the product id.

Codeigniter Update cart

I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>
Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html

Categories