Hey guys i have a bit of a problem. I don’t really know whats going on with my code… Its all working except for this quantity update thing. I was wondering if you could have a look and check whats up.
Its meant to go so that jQuery updates the input box although thats limited by the stock in the database, this part works fine although the problem is when i try to submit that data into the database… I was really trying to use AJAX as what i needed is that upon the submission of the jquery code (on focusout) it would run this mysql script with the php variables sent through although this didn’t for some reason work… I really couldn’t get it working, so this at the moment submits the form. I was thinking of a header linking it back with some variables yet that just doesn’t seem like that would work, as when updated i would like it to keep that quantity in there.
Heres my Code.
Basket.php
[php]
<table cellspacing="0" style="width:100%; margin-top:60px;">
<tr>
<td style="width:215px"><h3>Quantity</h3></td>
<td><h3>Product Name</h3></td>
<td style="width:200px"><h3>Price</h3></td>
</tr>
<?php
$cookie1 = $_COOKIE['Basket_Cookie'];
$cookie2 = $_COOKIE['User_Cookie'];
$cart_query = mysql_query("SELECT * FROM shopping_cart
INNER JOIN products ON products.id = shopping_cart.product_id
WHERE cookie_var1 = '".$cookie1."' AND cookie_var2 = '".$cookie2."'");
$a = 0;
$i = 0;
$redirect = 0;
echo '<form action="delivery_details.php" method="POST">';
while($cart1 = mysql_fetch_assoc($cart_query)){
$inv_id = $cart1['id'];
$redirect++;
if($cart1['stock_levels'] > 0){
$stock = '$("#quantity'.$cart1['id'].'").val("1"); $("#total'.$cart1['id'].'").val(db_price1); $("#totals'.$cart1['id'].'").val(db_price2);';
} else {
$stock = '$("#quantity'.$cart1['id'].'").val("0");';
};
echo '
<tr style="background-color:#F8D6E6;">
<script>
$(document).ready(function() {
var db_price1 = '.$cart1['product_value1'].';
var db_price2 = '.$cart1['product_value2'].';
'.$stock.'
$("#quantity'.$cart1['id'].'").focusout(function(event) {
var value = $(this).val();
var db_val = '.$cart1['stock_levels'].';
var db_price = '.$cart1['product_value1'].';
var db_price2 = '.$cart1['product_value2'].';
if(value > db_val)
{
$(this).val(db_val);
$("#total'.$cart1['id'].'").val(db_val * db_price);
$("#totals'.$cart1['id'].'").val(db_val * db_price2);
$("#total_q'.$cart1['id'].'").val(db_val);
$("#quantity_products").submit();
} else {
$("#total'.$cart1['id'].'").val(value * db_price);
$("#totals'.$cart1['id'].'").val(value * db_price2);
$("#total_q'.$cart1['id'].'").val(value);
$("#quantity_products").submit();
}
});
});
</script>
<input type="hidden" class="math" id="total'.$cart1['id'].'"/>
<input type="hidden" class="math2" id="totals'.$cart1['id'].'"/>
<td><h4 style="font-size:10px;"><input type="text" name="quantity" class="quantity" id="quantity'.$cart1['id'].'"/> (Stock Level: '.$cart1['stock_levels'].')</h4></td>
<td><h4>'.$cart1['product_name'].'</h4></td>
<td><h4 style="float:left">£'.$cart1['product_value1'].'.'.$cart1['product_value2'].' </h4>
<input type="submit" name="remove" class="remove" value="Remove"/>
<input type="hidden" value="'.$cart1['id'].'" name="remove_var"/>
</td>
</tr>
<tr style="background-color:#FFF; height:2px;">
<td></td>
<td></td>
<td></td>
</tr>
';
echo "<form action='content/process.php' method='POST' id='quantity_products'>
<input type='text' id='total_q".$cart1['id']."' name='count_id' value='1'/>
<input type='text' name='count_var' value='".$cart1['id']."'/>
</form>";
}
if($redirect == 0){
header('Location: products.php');
}
$vat_query = mysql_query("SELECT * FROM preferences");
while($vat_row = mysql_fetch_assoc($vat_query)){
$vat = $vat_row['vat_price'];
$del1 = $vat_row['delivery_price1'];
$del2 = $vat_row['delivery_price2'];
}
echo '
<script>
$(document).ready(function() {
checkTotal();
$(".quantity").focusout(
function(){
checkTotal();
}
);
function checkTotal(){
var sum = 0;
$(".math").each(function() {
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
var sum2 = 0;
$(".math2").each(function() {
if(!isNaN(this.value) && this.value.length!=0) {
sum2 += parseFloat(this.value);
}
});
var b4html1 = sum * 100;
var b4html2 = b4html1 + sum2;
var b4html2_string = b4html2.toString();
var pennies = b4html2_string.substr(b4html2_string.length -2, b4html2_string.length);
var pounds = b4html2_string.substr(0, b4html2_string.length -2);
$("#sub_total").val("£" + pounds + "." + pennies);
var vat1 = pounds + pennies;
var vat1_string = vat1.toString();
var vat2 = '.$vat.' / 100 ;
var vat3 = vat1 * vat2 / 100;
var vat4 = parseFloat(vat3).toFixed(2)
$("#vat_total").val("£" + vat4);
var final1 = pounds * 100;
var final2 = (final1 * 1) + (pennies * 1);
var final2_5 = vat4 * 100;
var final3 = (final2 * 1) + (final2_5 * 1);
var final3_5 = '.$del1.' * 100;
var final4 = (final3_5 * 1) + ('.$del2.')
var final5 = (final4 * 1) + (final3 * 1);
var final6 = final5.toString();
var final6_pennies = final6.substr(final6.length -2, final6.length);
var final6_pounds = final6.substr(0, final6.length -2);
var final7 = "£" + final6_pounds + "." + final6_pennies;
var final8 = final6_pounds + "." + final6_pennies;
$("#final_total").val(final7);
$("#final_total2").val(final8);
}
});
</script>
';
?>
<tr style="height:1px;">
<td></td>
<td></td>
<td></td>
</tr>
<?php
echo '
<tr style="background-color:#ECA3C9;">
<td></td>
<td><h5 style="float:right; font-weight:bold">Subtotal:</h5></td>
<td><h5><input type="text" class="hide1" id="sub_total" disabled="disabled" style="font-weight:bold"/></h5></td>
</tr>
<tr>
<td></td>
<td><h5 style="float:right">VAT:</h5></td>
<td><h5><input type="text" class="hide2" id="vat_total" disabled="disabled"/></h5></td>
</tr>
<tr>
<td></td>
<td><h5 style="float:right">Delivery:</h5></td>
<td><h5><input type="text" class="hide2" id="delivery" disabled="disabled" value="£'.$del1.".".$del2.'"/></h5></td>
</tr>
<tr>
<td></td>
<td><h5 style="float:right">Invoice Amount:</h5></td>
<input type="hidden" id="final_total2" name="f_total"/>
<td><h6><input type="text" class="hide3" id="final_total" disabled="disabled"/></h6></td>
</tr>
';
?>
<tr style="height:30px;">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><a href='products.php'><img src="images/layout/con_shopping.jpg" style="float:left" id="shopping" alt="Continue Shopping"/></a></td>
<td><img src="images/layout/paypal_logo.jpg" style="float:right" alt="Paypal"></td>
<td><input type="submit" id="checkout"></td>
</tr>
</table>
</form>
Process.php
[php]
include("…/…/db_connect/db_con.php");
$count_id = $_POST[‘count_id’];
$count_var = $_POST[‘count_var’];
header(“Location:…/basket.php”);
mysql_query(“UPDATE shopping_cart SET quantity = '”.$count_id."’ WHERE product_id = ‘".$count_var."’");
[/php]