...

View Full Version : How do I change a fixed item count on a checkcart page to a user adjustable one?



judgedredd
05-18-2011, 02:04 PM
I am trying to tweak code that I did not write in a checkcart.php page.

In the checkcart page the quantity field is not able to be changed and I would like to tweak the code to enable a buyer to change the quantity with an update button added.

Can someone show me how to do it?

Here is the code that is in the checkcart.php page, and the quantity area is shown in bold as 'QUANTITY AREA HERE':
<?php
session_start();
require("./UserSQL.inc.php");

$db = new UserSQL($DBName);
$filename = $_GET["filename"];
$filename = explode("/",$filename);
$count = count($filename)-1;
$filename = $filename[$count];
$catid = $db->getcatidbyfilename($filename);
if(!empty($catid)){
$sql="update catalog set count=count+1 where catalogid='$catid'";
$db->update($sql);
}
$result = $db->getcatalogbyid($catid);
$catalogname=$result[0]["catalogname"];
$catalogname=stripslashes($catalogname);
$title = $result[0]["title"];
$title=stripslashes($title);
$title=html_entity_decode($title);
$title=strip_tags($title);
$description=$result[0]["description"];
$keywords=$result[0]["keywords"];
$bodytext = stripslashes($result[0]["bodytext"]);
$bodytext = html_entity_decode($bodytext);
$filepath="./pics/";
if(!empty($productkey))
{
unset($Product[$productkey]);
session_register("Product");
$_SESSION["Product"]=$Product;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title> Checking your order with Gum Paste Flowers by Suzette</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="<?php echo $keywords;?>" />
<meta name="description" content="<?php echo $description;?>" />

<link rel="stylesheet" type="text/css" href="style-css-test-page.css" />

<!--[if gte IE 5.5]>
<script language="JavaScript" src="dhtml.js" type="text/JavaScript"></script>
<![endif]-->

</head>
<body>
<table align="center" width="100%" valign="top" border="0" cellspacing="0" cellpadding="0">
<tr>
<!-- Table 1 Row 1 Column 1 -->
<td valign="top">


<table align="center" valign="top" width="820" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<!-- Table 2 Row 1 Column 1 -->
<td width="69" id=table2>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</td>
<!-- Row 1 Column 2 -->
<td id=verticalborder>
&nbsp;
</td>
<!-- Row 1 Column 3 -->
<td id=centralbody>

<table width="820" height="678" align="center" border="0" cellspacing="0" cellpadding="0">
<tr height="130"><td align="center" valign="top">
<div id="logo" ><?php include("header.php"); ?></div>
</td></tr><tr height="30"><td align="center" valign="top">
<div id="header">
<div id="menu"><?php include("topmenu.php"); ?></div>
</div>
</td></tr>

<!-- Start of Central Text/Content -->

<tr height="450"><td Valign="top">
<form action="<? print "$PHP_SELF";?>" method="post">
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" bgcolor="#FDFDF0">

<tr>
<td colspan="7" align="right"><A HREF="flowers.php"><img src="images/Cont_0.jpg" border="0"></A>&nbsp;&nbsp;</td>
<td align="right"><a href="checkout.php?<?print"areaid=$areaid&taxid=$taxid";?>"><img src="./images/checkout.jpg" border="0"></a><td></tr>
<tr><td colspan="8">&nbsp;&nbsp;&nbsp;Country:&nbsp;&nbsp;&nbsp;
<select name="areaid" onchange="submit()">
<?
$areares=$db->getarea();
if(!empty($areares)){
while(list($key,$val)=each($areares)){
$tempareaid=stripslashes($val["areaid"]);
$temparea=stripslashes($val["area"]);
if($areaid==$tempareaid){
print"<option value=\"$tempareaid\" selected>$temparea</option>";
}else{
print"<option value=\"$tempareaid\">$temparea</option>";
}
}
}
?>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;Location:&nbsp;&nbsp;<select name="taxid" onchange="submit()">
<?
$taxres=$db->gettax();
if(!empty($taxres)){
while(list($key,$val)=each($taxres)){
$temptaxid=stripslashes($val["taxid"]);
$templocation=stripslashes($val["location"]);
if($taxid==$temptaxid){
print"<option value=\"$temptaxid\" selected>$templocation</option>";
}else{
print"<option value=\"$temptaxid\">$templocation</option>";
}
}
}
?></select>
</td></tr>
<?
if(empty($areaid)){
$areaid=$areares[0]["areaid"];
}
$rate=$db->getratebyid($areaid);
if(empty($taxid)){
$taxid=$taxres[0]["taxid"];
}
$taxratio=$db->gettaxbyid($taxid);
?>
<tr height="35">
<td width="15%"><center><b>Flower</b></center></td>
<td width="255"><center><b>Description</b></center></td>
<td width="115"><center><b>Size/Style</b></center></tdf>
<td><center><b>Quantity</b></center></td>
<td><center><b>Price</b></center></td>
<td width="55"><center><b>Tax</b></center></td>
<td width="70"><center><b>Sub-<br>Total</b></center></td>
<td></td></tr>

/*QUANTITY AREA HERE */

<?php
$total=0;

if(session_is_registered("Product")){
foreach ($Product as $i => $value){
$productarray=$Product[$i];
$productid=$productarray[0];
$sizeid=$productarray[1];
$quantity=$productarray[2];

$productres=$db->getproductbyid($productid);
if(!empty($productres)){
$description=stripslashes($productres[0]['description']);
$sizename=stripslashes($productres[0]['sizename']);
$sizename=$sizearray[$sizename];
$picture=stripslashes($productres[0]['picture']);
$smallpic=stripslashes($productres[0]['smallpic']);

$sizeres=$db->getproductsizebyid($sizeid);
$size=stripslashes($sizeres[0]['size']);
$price=stripslashes($sizeres[0]['price']);


$tax=$price*$quantity*$taxratio;
$tax=round($tax,2);
$totalprice=$price*$quantity+$tax;

$total+=$totalprice;

$price=number_format($price, 2, '.', '');
$tax=number_format($tax, 2, '.', '');
$totalprice=number_format($totalprice, 2, '.', '');

?>
<tr bgcolor="#FFFFFF">
<td><center><img src="<? print"$filepath$smallpic";?>" border="0"></center></td>
<td><center><? print"$description";?></center></td>
<td><center><? print"$size";?></center></td>
<td><center><? print"$quantity";?></center></td>
<td><center><? print"$price";?></center></td>
<td><center><?print"$tax";?></center></td>
<td align="right">$<? print"$totalprice";?></td>
<td width="90"><center><a href="<? print "$PHP_SELF?productkey=$i";?>">Remove</a></center></td></tr>


<?php
}}
$total+=$rate;
$rate=number_format($rate, 2, '.', '');
$total=number_format($total, 2, '.', '');
print"<tr>
<td colspan=\"6\" align=\"right\">Shipping/Packing:<br>
Total:<br></td>
<td align=\"right\">$rate<br>$$total<br></td>
<td></td></tr>";
}
?>
<tr><td colspan="8">&nbsp;</td></tr>
<tr><td colspan="7" align="right"><A HREF="flowers.php"><img src="images/Cont_0.jpg" border="0"></A>&nbsp;&nbsp;</td><td align="right"><a href="checkout.php?areaid=$areaid&taxid=$taxid"><img src="./images/checkout.jpg" border="0"></a></td></tr>
</table>
</form>
</td></tr>
<tr height="10">
<td>
&nbsp;
</td>
</tr>
<tr height="68">
<td align="center" valign="top">
<div id="footer"><?php include("footer.php"); ?></div>
</td></tr>
</table>
</td>
<td id=verticalborder>
&nbsp;
</td>
<td width="69" id=table3>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</td>

</tr>
</table>
</td>
</tr>
</table>



<!-- End of footers -->
<!-- Start of Address Area -->
<!-- End of Wrapper -->

<!-- Start of StatCounter Code -->
<script type="text/javascript">
var sc_project=6371968;
var sc_invisible=1;
var sc_security="5be6e313";
</script>

<script type="text/javascript"
src="http://www.statcounter.com/counter/counter.js"></script><noscript><div
class="statcounter"><a title="drupal analytics"
href="http://statcounter.com/drupal/" target="_blank"><img
class="statcounter"
src="http://c.statcounter.com/6371968/0/5be6e313/1/"
alt="drupal analytics" ></a></div></noscript>
<!-- End of StatCounter Code -->

</body>
</html>

mlseim
05-18-2011, 02:53 PM
I would expect that the quantity would be changed using a textbox similar to the red part shown below ...

<tr bgcolor="#FFFFFF">
<td><center><img src="<? print"$filepath$smallpic";?>" border="0"></center></td>
<td><center><? print"$description";?></center></td>
<td><center><? print"$size";?></center></td>
<td><center><input type="text" name="qty" value="<?=$quantity?>"></center></td>
<td><center><? print"$price";?></center></td>
<td><center><?print"$tax";?></center></td>
<td align="right">$<? print"$totalprice";?></td>


When displaying the form, the user would be able to adjust qty.
To affect the price immediately, would require javascripting (or AJAX).
But, if they changed the quantity and submitted the form, you would
then take the new quantity value and recalculate.

It would be nice to see how your form currently works (a working example).


.

judgedredd
05-18-2011, 03:03 PM
Hi mlseim,

Thanks for that.

I do know that in the code there is the ability to remove an item. When an item is removed then the page refreshes immediately with the new list of (reduced) number of items.

The line where the removal occurs is:


<td width="90"><center><a href="<? print "$PHP_SELF?productkey=$i";?>">Remove</a></center></td></tr>So is the update code exactly the same?

judgedredd
05-18-2011, 03:07 PM
Oops. Forgot to give you the link for the actual site to show you how the form works. Here it is:

http://www.gumpasteflowersbysuzette.com/flowers.php

choose some flowers and then proceed to 'View my Shopping'.

judgedredd
05-18-2011, 03:24 PM
Could I add something like the code below?:


<form name="<?print"$productid";?>" action="<? print"$PHP_SELF";?>" method="post">
<input type="submit" name="updateproduct" value="Update">


Not sure how to add the "updateproduct" field to the code. Or is that not needed? I have altered some values here, from the page which lists the products/flowers to reflect what this code is trying to do.

judgedredd
05-18-2011, 04:44 PM
mlseim,

If I change the number of items and then go to checkout it does not take the new value. It reverts back to the original value.

judgedredd
05-18-2011, 07:49 PM
I just found the answer to updating after changing the amount of items in the checkcart page.

I have to have an 'Update' button which is wrapped in a <form></form>.

Do you know how to do this?

mlseim
05-18-2011, 09:28 PM
In the link you gave, when I enter a quantity and "add to cart",
I get a "page not found" error. So, I have not yet seen the page
where you wish to change the quantity.


.

judgedredd
05-18-2011, 09:49 PM
Hi mlseim,

Thank you!! The website was hacked sometime between yesterday and the 10th May, so what you saw was the result of that.

If it was not for you I would not have noticed this.

I am having my hosting company look into this as I write.

So I will let you know when this has been sorted out.

Thanks again.

judgedredd
05-19-2011, 01:07 AM
Hi mlseim,

The site has been sorted out so all the pages work.

I have added your little snippet of code and you can see it in operation.

I have been researching how to reload page with new values and is a bit beyond me because I have to make sense of the code I found. It is not as easy to do as I thought.

mlseim
05-19-2011, 02:41 AM
Various ways of hacking ...

One might be MySQL injections because you don't sanitize variables
before using them in your queries:
http://php.net/manual/en/function.mysql-real-escape-string.php

Another might be directory and file permissions set to 0777 (777).
They should not be set to that. Perhaps, 744 or 644?
Depends sort of on your webhost server ... how they set the permissions
when files are created or updated. Ask your webhost about that.


.

judgedredd
05-19-2011, 03:01 AM
Not really sure why I was sent to the page you gave as it did not seem to have anything to do with this update code.

I found this on some site:


if (isset($_POST['submit'])){
foreach($_POST as $key => $value){
$key = explode("-",$key);
$key = end($key);
$key = explode("submit", $key);
$key = end($key);

if($_POST['quantity-'.$key] == 0){
unset($_SESSION['cart'] [$key]);
//echo "Unset session";
}else{
$_SESSION['cart'][$key]['quantity'] = $value;
//echo "Updated session";
}

}
}But not really sure how to implement it as the
'cart'variable is not in my code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum