Jquery auto-compute - php

My goal is to be able to compute the subtotal from given 2 values. First value is given, and the other one is inputted. The subtotal changes when the user inputs a different value into the quantity field.
Here's the jquery code that I'm using:
<script type="text/javascript">
$(function(){
$('input[id^=qty]').keyup(function(){
var parentDiv = $(this).closest('#axd');
var curprice=parentDiv.find('input[id^=price]').val();
var curqty= this.value;
var curtotal=curprice * curqty;
parentDiv.find('input[id^=comp]').val(curtotal);
});
});
And here's the body:
<tr>
<form name="cartform" method="get" onsubmit="return validate_form(this);" action="checkout.php">
<div id="axd">
<td><?php echo $name; ?></td>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $id; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $qtyhand; ?>"/>
<input type="hidden" name="dprice[]" value="<?php echo $dsprice; ?>"/>
<td><?php echo $qtyhand; ?></td>
<td><input type="text" id="<?php echo 'qty' . $id; ?>" name="qbuys[]" value=""/></td>
<input type="hidden" id="<?php echo 'price' . $id; ?>" name="sprice[]" value="<?php echo $ssprice; ?>"/>
<td><?php echo $ssprice; ?></td>
<td><input type="text" name="subtot[]" id="<?php echo 'comp' . $id; ?>" value=""/></td>
<td><img src="../img/system/delete-icon.png"></img></td>
</div>
</form>
</tr>
The error is that it doesn't output any value at all. What's wrong with my code?
Here's the link to the full version of the code: http://cu.pastebin.com/sWxFV6Hg

why not just assign each field an ID and use the classic javascript instead of jquery for one multiply and a sum.
go like this: - assign ids then write this function:
function operation()
{
var op1 = document.getElementById('id1').value;
var op2 = document.getElementById('id2').value;
document.getElementById('wherever_you_want_it').value = op1 (whatever operation) op2;
}
Call the function on whatever event you want. And... done it.

Related

Form send data to php with multiple inputs with the same name

I'm trying make a form where me or user can insert data. Some parts are coming form data base like: Work name and work price. In input fields can insert work amount. In each input row have checkbox, if checkbox ar checked, that row will be seen in php.
<table class="table">
<thead>
<tr>
<th scope="col">*</th>
<th scope="col">Work name</th>
<th scope="col">Quantity</th>
<th scope="col">Price for unit</th>
<th scope="col">Total price</th>
</tr>
</thead>
<form method="POST" action="process/pdf.process.php">
<tbody>
<?php
$works = ORM::for_table('work')->find_many();
foreach($works as $work){
?>
<tr id="<?php echo $work->id; ?>">
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<td><?php echo $work->name; ?></td>
<td><input type="text" placeholder="" class="amount"/> <?php echo $work->unit; ?></td>
<td class="work_price" data="<?php echo $work->price; ?>"><?php echo $work->price.'€/'.$work->unit; ?></td>
<td class="total_price">0€</td>
<input type="" name="work_id[]" value="<?php echo $work->id; ?>" />
<input type="hidden" name="work_name[]" value="<?php echo $work->name; ?>" />
<input type="hidden" name="amount[]" class="<?php echo $work->id; ?>_copy_amount" value="" />
<input type="hidden" name="unit[]" value="<?php echo $work->unit; ?>" />
<input type="hidden" name="unit_price[]" value="<?php echo $work->price; ?>€" />
<input type="hidden" name="unit_total[]" class="<?php echo $work->id; ?>_copy_total" value="" />
</tr>
<?php
}
?>
</tbody>
<input type="submit" name="do_pdf" value="Pga jkāuztaisa ar jquery" />
</form>
</table>
Now, there is php, but how can i show only checked rows in while loop?
<?php
$data = array();
$work_id = array();
$work_names = $_POST['work_name'];
$amounts = $_POST['amount'];
$units = $_POST['unit'];
$units_prices = $_POST['unit_price'];
$units_total = $_POST['unit_total'];
if(isset($_POST['useit'])){
$work_id = $_POST['useit'];
}
$data = array($work_id, $work_names, $amounts, $units, $units_prices, $units_total);
echo '<pre>';
echo htmlspecialchars(print_r($data, true));
echo '</pre>';
?>
There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)
form (note that i gave work_name the id as an index, use_it not (but it could have)
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />
The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this
foreach($_POST['useit'] as $work_id){
$work_name = $work_names[$work_id];
//do something with the checked rows only (all others are not looped over)
}
This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>].
A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.

How can i keep data array from checkbox when datalist has many page

My problem is array can keep data only present page.
If user selected data page 1 & page 4 and push submit when he uses page 4
My array will keep data from page 4.
I want to keep data from the page that the user selects every page.
datalist table page
<td>
<center>
<input type="hidden" name="txtID[<?php echo $ven2["id"]; ?>]" id="txtID" value="<?php echo $ven2["id"]; ?>"><?php echo $ven2["id"]; ?>
</center>
</td>
<td>
<center>
<input type="hidden" name="txtACC[<?php echo $ven2["id"]; ?>]" id="txtACC" value="<?php echo $ven2["acc_name"]; ?>"><?php echo $ven2["acc_name"]; ?>
</center>
</td>
<td>
<input type="hidden" name="txtITM[<?php echo $ven2["id"]; ?>]" id="txtITM" value="<?php echo $ven2["item_name"]; ?>"><?php echo $ven2["item_name"]; ?>
</td>
<td>
<center>
<input type="checkbox" name="dataset[<?php echo $ven2["id"]; ?>]" id="dataset" value="<?php echo $i++; ?>">
</center>
</td> 
show data page
foreach($_POST["dataset"] as $key=>$value)
{
if($_POST["dataset"][$key] != "")
{
$DATA_ID = $_POST["dataset"][$key];
$DATA_ACC = $_POST["txtACC"][$key];
$DATA_ITM = $_POST["txtITM"][$key];
print "ACCOUNTCODE = ".$DATA_ACC."<br>";
print "ITEM = ".$DATA_ITM."<br>";
print "========================== <p>";
}
}
My web template
<script type="text/javascript" src="jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.dataTables.min.css">

find the checkbox in table and check the checkbox on enter key press

how can i focus on the highlighted row checkbox and on enter key make the checkbox checked.
when i hit enter all the check box is checked but i want to check the checkbox of highlighted row.
i want this highlighted row's checkbox to be checked on enter key press
<tbody id="navTable">
<tr class="lastRow" id="start">
<td>
<div class="checkbox">
<label><input type="checkbox" id="selectedProduct" name="selectBox" value="<?php echo ucfirst($row['pid']); ?>"></label>
</div>
</td>
<td><?php echo ucfirst($row['Product_Name']); ?><input type="text" name="productName[]" value="<?php echo ucfirst($row['Product_Name']); ?>" hidden="true"></td>
<td><input type="text" name="pPrice" value="<?php echo ucfirst($row['Product_Price']); ?>" hidden="true"><?php echo $row['Product_Price']; ?></td>
<td><input type="text" name="pStack" value="<?php echo ucfirst($row['Product_Stack_No']); ?>" hidden="true"><?php echo $row['Product_Stack_No']; ?></td>
<td><input type="text" value="<?php echo ucfirst($row['Product_Purchased']); ?>" hidden="true"><?php echo ($row['Product_Purchased'] - $row['Product_Sold']); ?></td>
<td><input type="text" value="<?php echo ucfirst($row['Product_GST_Rate']); ?>" hidden="true"><?php echo $row['Product_GST_Rate']."%"; ?></td>
<td><input type="text" value="<?php echo ucfirst($row['Product_Purchase_Date']); ?>" hidden="true"><?php echo $row['Product_Purchase_Date']; ?></td>
<td><input type="text" value="<?php echo ucfirst($row['Product_Expiry_Date']); ?>" hidden="true"><?php echo $row['Product_Expiry_Date']; ?></td>
</tr>
window.displayBoxIndex = -1;
var Navigate = function (diff) {
displayBoxIndex += diff;
var oBoxCollection = $("#navTable tr");
if (displayBoxIndex >= oBoxCollection.length) {
displayBoxIndex = 0;
}
if (displayBoxIndex < 0) {
displayBoxIndex = oBoxCollection.length - 1;
}
var cssClass = "navBox";
oBoxCollection.removeClass(cssClass).eq(displayBoxIndex).addClass(cssClass);
}
$(document).on('keypress keyup', function (e) {
if (e.keyCode == 13) {
//alert("hello");
$('.navBox').trigger('click');
$("input:checkbox").click();
return false;
}
if (e.keyCode == 40) {
//down arrow
Navigate(1);
}
if (e.keyCode == 38) {
//up arrow
Navigate(-1);
}
});

GET single loop form data in hyperlink php

I am trying to update quantity as i fill the quantity field and click on update button. It works but it shows the form fields of each item in the loop and works for the last entry update only. Something like this.
/EcommerceClient/index.php?page=cart&action=update&id=3&name=%09%0D%0ACool+T-shirt&color=blue&size=XL&quantity=4&id=4&name=HBD+T-Shirt&color=yellow&size=XL&quantity=900
In the above link it should only get the information associated with id=3 only because i tried to update the quantity of id=3.
Here is my code. Any suggestions or help will be highly appreciated.
Code
if(isset($_GET['action']) && $_GET['action']=="update"){
$id= intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
$qty = $_GET['quantity'];
$index = $id." ".$color. " ".$size;
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']=$qty;
print_r($_SESSION['cart'][$index]);//It just shows me the last item array.
}
}
?>
<form class="product" method="get" action="index.php">
<table>
<input type="hidden" name="page" value="cart">
<input type="hidden" name="action" value="update">
<?php
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $value){
?>
<tr>
<input type="hidden" name="id" value="<?php echo $value['id'] ?>">
<input type="hidden" name="name" value="<?php echo $value['name'] ?>">
<input type="hidden" name="color" value="<?php echo $value['color'] ?>">
<input type="hidden" name="size" value="<?php echo $value['size'] ?>">
<td><?php echo $value['id'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['price']?> </td>
<td><?php echo $value['quantity']?><input type="text" name="quantity" value="<?php echo $value['quantity'] ?>"></td>
<td><?php echo $value['color'];?> </td>
<td><?php echo $value['size']; ?></td>
<td><?php echo "$" .$value['price']*$value['quantity']. ".00"; ?>
</tr>
<?php
}
}
?>
<tr><td><button type="submit">Update</button></td></tr>
</table>
</form>
You can update the quantity by not posting or getting all the cart product. Either you can use simple JS to update the field value or use SESSION variable array to update data as #Marc B mentioned.

send form data with serialize to php page : $_POST don't get data

Im getting crazy with this problem.
I try to use ajax to send data from a form in a way that I have not to reload all webpage (my webpage contain some queries for generate index menu that produce traffic); I create sendForm method, which handle form datas and pass it to php page to process.
This is the js code:
function sendForm(){
var http = false;
http = new XMLHttpRequest();
var ser = $(":input").serialize();
http.open("POST", "http://localhost/redir.php" ,true);
http.onreadystatechange= handleResponse();
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", ser.length);
http.setRequestHeader("Connection", "close");
http.send(ser);
return true;
}
My form data contain a variable numbers of inputs because there's some php code inside a for cycle.
<form name="form" id="form" class="table table-hover" method="post">
<?php while(.....)) { ?>
<tr style="line-height:40px">
<td><?php echo $home;?></td>
<td><?php echo $vis;?></td>
<td><input type="text" name="goal_h[]" class="input-mini"
<?php if ($sq[16] == "2"){ ?>
readonly value="<?php echo $goal_h;?>"
<?php }else if ($sq[16] == "3"){
?> readonly value="-"
<?php } else echo "value=\"-\"";
?> id="goal_h<?php echo $i?>[]" maxlength="2" size="2" />
<input type="hidden" name="id_sub" value="<?php echo $id_sub;?>" />
<input type="hidden" name="id" id="id" value="<?php echo $id_d;?>" />
<input type="hidden" name="data_gg" value="<?php echo $data_gg;?>" />
<input type="hidden" name="n_gg" value="<?php echo $n_gg;?>" />
<input type="hidden" name="id_p_home[]" id="idphome<?php echo $i?>" value="<?php echo $tmp_id_p;?>" />
<input type="hidden" name="id_p_vis[]" id="idpvis<?php echo $i?>" value="<?php echo $sq[10];?>" />
</td>
<td>
<input type="text" name="goal_v[]" class="input-mini" <?php if ($sq[16] == "2"){ ?> readonly value="<?php echo $goal_v;?>" <?php }else if ($sq[16] == "3"){ ?> readonly value="-" <?php } else echo "value=\"-\""; ?> id="goal_v<?php echo $i?>[]" maxlength="2" </td>
<td><input type="checkbox" <?php if ($sq[16] == "2"){ ?> checked <?php } ?> id="sosp<?PHP echo $i?>" name="sosp[]" value="<?PHP echo $i?>" onclick="cambiaStato(<?php echo $i?>);"/></td>
<td><input type="checkbox" <?php if ($sq[16] == "3"){ ?> checked <?php } ?> id="nd<?PHP echo $i?>" name="nd[]" value="<?PHP echo $i?>" onclick="cambiaStato(<?php echo $i?>);"/>
</td>
</tr>
<tr style="line-height:40px">
<td><input type="submit" id="submit" name="submit" value="AGGIORNA IL PUNTEGGIO" onclick="sendForm()" /></td>
<td colspan="5"></td>
</tr>
<?php } ?>
</form>
this code works if I don't use with ajax and simply use an action=name_of_file.php, but when I try to use sendForm() method, in redir.php file I lose $_POST variables.
$home_id_p=$_POST['id_p_home'];
$vis_id_p=$_POST['id_p_vis'];
$goal_home=$_POST['goal_h'];
$goal_vis= $_POST['goal_v'];
$sosp= (isset($_POST['sosp'])) ? $_POST['sosp'] : "99";
$nd= (isset($_POST['nd'])) ? $_POST['nd'] : "99";
and then I need to loop on $home_id_p.
I tried also wihout serialize(), and also with serializeArray() :
var ser = $(":input").serializeArray();
but with this I'm not able to send it, because I get an error "invalid argument" in
http.send(ser)
Any idea?
Thanks

Categories