Computing values on keyup using jQuery - php

The script below computes the subtotal and total of given items.
<script type="text/javascript">
$(function(){
$('input[type=button]').click(function(){
});
$('input[id^=qty]').keyup(function(){
var gtotal=0;
var parentDiv = $(this).closest('#korokor');
var curprice=parentDiv.find('input[id^=price]').val();
var curqty= this.value;
var curtotal=curprice * curqty;
parentDiv.find('input[id^=subtotal]').val(curtotal);
$('input[id^=subtotal]').each(function(index) {
var currentnum= $(this).val();
var intnum =parseInt(currentnum);
gtotal=gtotal + intnum;
});
$('input[name=supertotal]').val(gtotal);
$('input[name=payment]').val(gtotal);
});
});
</script>
<?php
$viewcarter=mysql_query("SELECT * FROM prod_table WHERE CURRP=1");
?>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>PRODUCT</th>
<th>QA</th>
<th>PRICE</th>
<th>QTY BUY</th>
<th>SUBTOTAL</th>
<th>REMOVE</th>
</tr>
</table>
<?php
if(mysql_num_rows($viewcarter)==0){
//no products
}else{
while($row=mysql_fetch_assoc($viewcarter)){
?>
<form name="blabber" method="get" action="expander.php">
<div id="korokor">
<table border="1">
<tr>
<td><?php echo $row['PID']; ?></td>
<td><?php echo $row['PRODUCT']; ?></td>
<td><?php echo $row['QTYHAND']; ?></td>
<td><?php echo $row['S_PRICE']; ?></td>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $row['PID']; ?>">
<input type="hidden" id="pname" name="product[]" value="<?php echo $row['PRODUCT']; ?>">
<input type="hidden" name="dprice[]" value="<?php echo $row['B_PRICE']; ?>">
<input type="hidden" name="sprice[]" id="<?php echo 'price'.$row['PID']; ?>" value="<?php echo $row['S_PRICE']; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $row['QTYHAND']; ?>"/>
<td><input type="text" name="qbuys[]" id="<?php echo 'qty'.$row['PID']; ?>"/></td>
<td><input type="text" name="subtotal[]" id="<?php echo 'subtotal'.$row['PID']; ?>"/></td>
<td><img src="delete-icon.png"></img></td>
</tr>
</table>
</div>
<?php
}
?>
Grand Total:<input type="text" name="supertotal" value=""/><br/>
Customer:<input type="text" name="customer" value=""/><br/>
Payment:<input type="text" name="payment" value=""/><br/>
<input type="submit" value="sell"/>
</form>
<?php
}
?>
It works, but I have a little problem with its appearance:
I tried to place them all in one table to make it more pleasing but the script which computes the values at keyup no longer works. I need a little help in modifying the appearance of the interface.

Use a single table...
<form name="blabber" method="get" action="expander.php">
<div id="korokor">
<table border="1">
<tr>
<th>ID</th>
<th>PRODUCT</th>
<th>QA</th>
<th>PRICE</th>
<th>QTY BUY</th>
<th>SUBTOTAL</th>
<th>REMOVE</th>
</tr>
<?php
if(mysql_num_rows($viewcarter)==0){
//no products
}else{
while($row=mysql_fetch_assoc($viewcarter)){
?>
//now the loop adds a new row rather than new table for each item in the cart.
<tr class="cartItem">
<td><?php echo $row['PID']; ?></td>
<td><?php echo $row['PRODUCT']; ?></td>
<td class="qnty"><?php echo $row['QTYHAND']; ?></td>
<td class="unitPrice"><?php echo $row['S_PRICE']; ?></td>
<td><input type="text" name="qbuys[]" id="<?php echo 'qty'.$row['PID']; ?>"/></td>
<td><input type="text" name="subtotal[]" id="<?php echo 'subtotal'.$row['PID']; ?>"/></td>
<td><img src="delete-icon.png"></img>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $row['PID']; ?>">
<input type="hidden" id="pname" name="product[]" value="<?php echo $row['PRODUCT']; ?>">
<input type="hidden" name="dprice[]" value="<?php echo $row['B_PRICE']; ?>">
<input type="hidden" name="sprice[]" id="<?php echo 'price'.$row['PID']; ?>" value="<?php echo $row['S_PRICE']; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $row['QTYHAND']; ?>"/>
</td>
</tr>
<?php
}
?>
</table>
</div>
That should fix much of the layout, having loads of tables as you were trying to do is just not a good idea. If the javascript is not working with a single table then it should be adapted so that it does.
I will take a look at that now...
This is only very rough im afraid (contact lenses dried out all of a sudden).. but
var runningSub = 0;
var total = 0;
$('.cartItem').each(function(){
var qnty = $('this .qnty').html();
var unitPrice = $('this .unitPrice').html();
runningSub + (qnty * unitPrice);
)};
// the cart items have been processed so..
if(runningSub >0){
total = (calc for the total price)
}
// Now you can set your values..
If you run the above with the additions needed to work out the final total and to put the values onto your page you might have something that works.. or at least a good idea of a direction to go in to get there..
please look at the HTML for the table as I have amended it to make the javascript simpler to write.

Related

Update shopping cart quantity PHP

I'm building a test shopping site using PHP / mySQL
here is my code:
the show_product.php:
<form method ="get" action="addtocart.php">
<input type="hidden" name="id_product" value='.$row['id_product'].'>
<tr>
<th colspan="2">'.$row['name'].'</th>
</tr>
<td>
<img align="center" src='.$row['photo'].' alt="">
</td>
<td>'.mb_substr($row['description'],0,200).'....</td>
<tr></tr>
<td>
'.$row['price'].'€<input type="number" name="quantity" value="1" min="1" max="20">
</td>
<td> <button type="submit" class="btn">add to cart</button></form>
the addtocart.php
session_start()
if(isset($_GET) & !empty($_GET)){
$id_product = $_GET['id_product'];
if(isset($_GET['quantity']) & !empty($_GET['quantity'])){ $quant = $_GET['quantity']; }else{ $quant = 1;}
$_SESSION['cart'][$id_product] = array("quantity" => $quant);
header('location: cart.php');
}else{
header('location: cart.php');
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";
?>
and some code of the cart.php
foreach ($cart as $key => $value) {
$cartsql = "SELECT * FROM product WHERE id_product=$key";
$cartres = mysqli_query($con, $cartsql);
$row = mysqli_fetch_assoc($cartres);
?>
<tr><form>
<td colspan="2"><?php echo $row['name'] ?></td>
<td><?php echo $row['price'].' €'; ?></td>
**<td><input type="number" class="quantity" name="quantity" value="<?php echo $value['quantity']; ?>"></td>**
<td><?php echo ($row['price']*$value['quantity']).' €'; ?> </td>
<td><img width="30" height="30" src="icons/delete.png" alt=""></td> </tr>
If i send to addcart an item of some quantity it's working.
the problem is I cannot change the quantity inside on cart.php, I know very few about Javascript.
Please any help how to do this ??
i did it with a submit form inside the cart :)
<form class="userform" method='get' name="change" action="addtocart.php">
<input type='hidden' name='id_product' value="<?php echo $row["id_product"]; ?>" />
<td><input size="5" type="number" name="quantity" value="<?php echo $value['quantity'] ?>" min="1" max="20" onchange="this.form.submit()"></td></form>

unable to get checkbox values

I have the following code. I want to get only selected checkbox values but I get only last checkbox value. see what's wrong in my code.
<div class="box-body table-responsive no-padding">
<h4>Select Jobwork</h4>
<table id="data-table" class="table table-hover jobworks-table table-responsive">
<thead>
<tr style="background-color: #DDDD; color:firebrick;">
<th><input type='checkbox' value="1" name="select_all" /></th>
<th>JobWork Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php foreach ($item as $key=>$value){
$decoded = json_decode($value['jobWorkJsonString']); ?>
<?php foreach($decoded as $row){ ?>
<tr class="odd gradeX">
<td><input type='checkbox' name="<?php echo $row->jobwork; ?>" /></td>
<td style="padding-left:0px;"><?php echo $row->jobwork; ?><input type="hidden" name="jobwork_name" value="<?php echo $row->jobwork; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->description; ?><input type="hidden" name="jobwork_description" value="<?php echo $row->description; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->jobPrice; ?><input type="hidden" name="price" value="<?php echo $row->jobPrice; ?>"></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
<br>
</div>
If you submit multiple fields with the same name, then PHP will only put the last one into $_POST / $_GET.
The exception is when you end the name with [] in which case it will generate an array instead.
However, checkboxes are only successful controls when checked, so simply adding [] to the names will cause the association between each set of date to be lost.
Instead of submitting all the data about each jobwork (you aren't trying to change it, otherwise you wouldn't be using hidden inputs!), put all the data you need into the checkbox input. Look up the rest of the data on the server.
<td><input type='checkbox' name="jobwork[]" value="<?= htmlspecialchars($row->id); ?>"/></td>
I've used $row->id as an example. I don't know how your data is structured on the server.
You will then be able to do:
foreach ($_POST['jobwork'] as $jobwork_id) {
$row = look_up_jobwork_by_id($jobwork_id);
}
… only the checked checkboxes values will appear in that array.
You have issue in the first checkbox code.
Name is given as value, both names should be same for the checkbox,
and get the request as an array
<?php foreach ($item as $key=>$value){
$decoded = json_decode($value['jobWorkJsonString']); ?>
<?php foreach($decoded as $row){ ?>
<tr class="odd gradeX">
<td><input type='checkbox' name="jobwork" value="<?php echo $row->jobwork; ?>" /></td>
<td style="padding-left:0px;"><?php echo $row->jobwork; ?><input type="hidden" name="jobwork_name" value="<?php echo $row->jobwork; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->description; ?><input type="hidden" name="jobwork_description" value="<?php echo $row->description; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->jobPrice; ?><input type="hidden" name="price" value="<?php echo $row->jobPrice; ?>"></td>
<?php } ?>
</tr>
<?php } ?>
get the value of job_work as an array, you will get the selected job_work

Using angular get value of selected check box that created by php dynamically

I want to get check box value that was created dynamically, I have searched about this but I didn't get any solution
<div ng-app="formListApp" ng-controller="formListController">
<form action="post" class="ng-pristine ng-valid">
<button class="md-raised" type="button" ng-transclude="" ng-click="applyTemplate()">Allpy Template</button>
<table>
<tr id="header" class="header_row_hed">
<td id="sl_no" class="sl_no_col_hed">Sl.No</td>
<td id="chk_box" class="chk_box_col_hed">
<input name="select_all" id="selectAll" class="" style="" ng-model="all" type="checkbox">
Select All
</td>
<td id="form_name" class="form_name_col_hed">Form Name</td>
<td id="template_name" class="template_name_col_hed">Applied template</td>
</tr>
<?php
$slNo = 1;
foreach ( $form_list as $form_lists ) {
<tr id="form-<?php echo $form_lists['id']; ?>" class="header_row">
<td id="<?php echo $slNo; ?>" class="sl_no_col"><?php echo $slNo; ?></td>
<td id="chk-box-<?php echo $form_lists['id']; ?>" class="sl_no_col_hed">
<input name="select_all" id="selectAll-<?php echo $form_lists['id']; ?>" class="" style=""
ng-checked="all" ng-model="formId<?php echo $slNo; ?>" type="checkbox"
val="<?php echo $form_lists['id']; ?>">
</td>
<td id="form-id-<?php echo $form_lists['id']; ?>"
class="form_name_col"><?php echo $form_lists['name']; ?></td>
<td class="template_name_col"></td>
</tr>
<?php } ?>
</table>
</form>
</div>
<script>
var myAdminApp = angular.module('formListApp');
myAdminApp.controller('formListController', function AdminController($scope) {
$scope.formId = {};
$scope.applyTemplate = function () {
}
});
</script>
this is my code I'm new to angular is it possible to get check box value?

How to add multiple rows of data from one sql table to another with selection based on checkboxes?

<form method="post">
<?php
$sql_u="SELECT * from cubaan";
$query_u = mysqli_query($conn,$sql_u);
while($row=mysqli_fetch_assoc($query_u)){
?>
<table border="2">
<tr>
<td><input type="checkbox" name="bio[]" value="<?php $row['name'];?>" data-valuetwo="<?php $row['age'];?>" data-valuethree="<?php $row['job'];?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['job'];?></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="post">
</form>
this is my code where I want to select data from the database and the selected data will insert to the new database
Use input type hidden as following
<form method="post">
<?php
$sql_u="SELECT * from cubaan";
$query_u = mysqli_query($conn,$sql_u);
while($row=mysqli_fetch_assoc($query_u)){
?>
<table border="2">
<tr>
<td>
<input type="checkbox" name="bio[<?php echo $row['id'];?>]" value="<?php echo $row['name'];?>">
<input type="hidden" name="age[<?php echo $row['id'];?>]" value="<?php echo $row['age'];?>">
<input type="hidden" name="job[<?php echo $row['id'];?>]" value="<?php echo $row['job'];?>">
</td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['job'];?></td>
</tr>
</table>
<?php
}
?>
<input type="submit" name="post">
</form>
And while your form submit action use
<?php foreach($_POST['bio'] as $id=>$name) {
echo $name;
echo $_POST['age'][$id];
echo $_POST['job'][$id];
}
?>

pass relevant hidden field values from the form to the controller

Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>

Categories