codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  PHP Shopping Basket  nyoung2005 at 09:34 on Friday, September 09, 2005
 

Hi,

I am creating a shopping basket and I am having problems with the delete_item function. It removes the items in the basket fine but when you click refresh on the browser it removes another from the basket and so forth.

I realise that it is because the method is being called again but wont the fact that $productID being passed in is different from that that was deleted.

Is it possible to validate the $productID before allowing to delete a product?

Neil

[php]
session_start();
header("Cache-control: private");
?>
<script type="text/javascript">
<!-- Hide script from old browsers -->
function productEnlarge(htmlPage, downScroll)
{
textWindow = window.open(htmlPage, "productEnglarge", "width=400, height=350, scrollbars=yes")
textWindow.focus()
setTimeout("textWindow.scroll(0, " + downScroll + ")", 1000)
}

if (document.layers)
{
document.captureEvents(Event.MOUSEDOWN)
}
document.onmousedown = captureMousedown

function captureMousedown(evt)
{
if (evt)
{
mouseClick = evt.which
}
else
{
mouseClick = window.event.button
}

if (mouseClick == 2 || mouseClick == 3)
{
alert("Right mouse click disabled for this page")
return false
}
}
// End hiding script from old browsers
</script>
<SCRIPT language=JavaScript>
function confirmDelete() {
if (confirm("Are you sure you would you like to clear your basket?")==false)
return false;
else
return true;
}
</script>
<?php

if (isset($_GET["cmd"]))
{
$productID = $_GET["productID"];
$title = $_GET["title"];
$description = $_GET["description"];
$image2 = $_GET["image2"];
}

$basket = new basket();

if (isset($_GET['deletecart']) && $_GET['deletecart'])
{
$basket -> delete_cart();
$empty = "<br/><p>Your sample basket has been emptied</p>";
$empty .= "<span class=\"pageLinks\"><p><a href=\"products1.php\">Continue Browsing</a></p></span>";

}
if (isset($_GET['deleteitem']) && $_GET['deleteitem'])
{
$basket -> delete_item($_GET['productID']);
}
if ($_GET["cmd"] == "addtocart")
{
$basket -> add_item($_GET["productID"], $_GET["title"], $_GET["description"], $_GET["image2"]);
}


?>

<?
class basket
{
function basket ()
{
if (!isset($_SESSION["item_count"]))
$_SESSION["item_count"] = 0;
}

function add_item($productID, $title, $description, $image2)
{
if ($_SESSION["item_count"] > 0)
{
$found=false;
for($i=0; $i < $_SESSION["item_count"]; $i++)
{
if ($_SESSION["items"][$i][0] == $productID) {
$found=true;
}
}

if (!$found)
{
$_SESSION["items"][$_SESSION["item_count"]] = array($productID, $title, $description, $image2);
$_SESSION["item_count"]++;
}
}
else
{
$_SESSION["items"][0] = array($productID, $title, $description, $image2);
$_SESSION["item_count"] = 1;
}
}

function show_cart()
{
include("dbProductsConnect.php");

if (isset($_SESSION["item_count"]) && $_SESSION["item_count"] > 0)
{
echo "<br/><p>Please find your sample basket below. <br/><br/>You can find instructions for using the sample basket <span class=\"pageLinks\"><a href=\"sampleBasketHelp.php\">here</a></span></p>";
echo "<table width=\"520\">";
echo "<tr><td style=\"border-bottom-width:2px; border-bottom-style:dotted; border-left-width:2px; border-left-style:dotted; border-top-width:2px; border-top-style:dotted\" align=\"center\"><b><p>Product</p></b></td><td align=\"center\" style=\"border-bottom-width:2px; border-bottom-style:dotted; border-top-width:2px; border-top-style:dotted\"><b><p>Product Image</p></b></td><td align=\"center\" style=\"border-bottom-width:2px; border-bottom-style:dotted; border-right-width:2px; border-right-style:dotted; border-top-width:2px; border-top-style:dotted\"><b><p>Remove</p></b></td></td></tr>";
echo "<tr><td style=\"border-bottom-width:2px; border-bottom-style:dotted; border-left-width:2px; border-left-style:dotted\" align=\"center\"><p class=\"basketSubs\">Click for product page</p></td><td align=\"center\" style=\"border-bottom-width:2px; border-bottom-style:dotted\"><p class=\"basketSubs\">Click to Enlarge</p></td><td align=\"center\" style=\"border-bottom-width:2px; border-bottom-style:dotted; border-right-width:2px; border-right-style:dotted\"><p class=\"basketSubs\">Click to Remove</p></td></tr>";
for($i=0; $i<$_SESSION["item_count"]; $i++)
{
$sql = "SELECT * FROM productlist where productID=". $_SESSION["items"][$i][0]."";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
$postedTitle = $row["title"];
$postedDescription = $row["description"];
$postedPrintArea = $row["printarea"];
$desc2 = $row["desc2"];
$postedLeadTime = $row["leadtime"];
$postedFastTrack = $row["fasttrack"];
$postedCategory = $row["category"];
$postedMinQuantity = $row["minquantity"];
$imageDisplay = $row["image2"];
$enlargeImage = $row["image2"];
$productID = $row["productID"];
$postedArtwork = $row["artwork"];
$sample = $row["sample"];

echo "<td style=\"border-bottom-width:2px; border-bottom-style:dotted; border-left-width:2px; border-left-style:dotted\" align=\"center\"><p><span class=\"pageLinks\"><a href='product.php?cmd=product&productID=$productID&category=$postedCategory&title=$postedTitle&description=$postedDescription&image2=$imageDisplay&printarea=$postedPrintArea&minquantity=$postedMinQuantity&fasttrack=$postedFastTrack&leadtime=$postedLeadTime&desc2=$desc2&artwork=$postedArtwork&sample=$sample'>".$_SESSION["items"][$i][1]."</a></span></span></p></td>";

$image = $_SESSION["items"][$i][3];

$imageExists = @GetImageSize($imageDisplay);

if($imageExists)
{
echo "<td style=\"border-bottom-width:2px; border-bottom-style:dotted\" align=\"center\"><a href=\"Javascript:productEnlarge('productEnlarge.php?enlargeImage=$imageDisplay')\"><img width=\"70\" border='0' height=\"70\" alt='Click to enlarge' src=\"$imageDisplay\"/></a></td>";
}
else
{
echo "<td style=\"border-bottom-width:2px; border-bottom-style:dotted\" align=\"center\"><img src=\"images/noImage.gif\" width=\"100\" border=\"0\"/></td>";
}
}
echo "<td style=\"border-bottom-width:2px; border-bottom-style:dotted; border-right-width:2px; border-right-style:dotted\" align=\"center\"><span class=\"pageLinks\"><p><a href=\"sampleBasket.php?deleteitem=1&productID=".$_SESSION["items"][$i][0]."\">";
echo "Remove</a></p></span></td>";
$physicalTitles[$i] = $_SESSION["items"][$i][1];
$virtualTitles[$i] = $_SESSION["items"][$i][1];
echo "</tr>";
}

$numElements = count($physicalTitles);

$basketItems = "";
$basketItems2 = "";

for($counter=0; $counter < $numElements; $counter++)
{
$basketItems .= $physicalTitles[$counter] . "\n";
$basketItems2 .= $physicalTitles[$counter] . "<br/>";
}

echo "<tr><td><br/><span class=\"pageLinks\"><p><a href=\"sampleBasket.php?deletecart=1\" onclick='return confirmDelete()'>Clear Basket</a></p></span><span class=\"pageLinks\"><p><a href=\"products1.php\">Continue Browsing</a></p></span></td>";

echo "<td align=\"center\" colspan=\"3\"><br/><br/>";

echo "<form action=\"sampleSendProcessPS.php\" method=\"post\">";
echo "<input type='hidden' name='basketItems' value='$basketItems'>";
echo "<input type='hidden' name='basketItems2' value='$basketItems2'>";
echo "<input name=\"submit\" class=\"buttonText\" type=\"submit\" value=\"Request Product Sample\">";
echo "</form>";

echo "<form action=\"sampleSendProcessVS.php\" method=\"post\">";
echo "<input type='hidden' name='basketItems' value='$basketItems'>";
echo "<input type='hidden' name='basketItems2' value='$basketItems2'>";
echo "<input name=\"submit\" class=\"buttonText\" type=\"submit\" value=\" Request Virtual Sample \">";
echo "</form>";

echo "</td></tr>";

echo "</table>";
}
else
{
echo "<p style=\"margin-top:20px\">There are currently no samples in your basket</p>";
echo "<span class=\"pageLinks\"><p><a href=\"products1.php\">Add Products</a></p></span>";
echo "<p>You can find instructions for using the sample basket <span class=\"pageLinks\"><a href=\"sampleBasketHelp.php\">here</a></span></p>";
}
}

function delete_item($productID)
{
$pos=0;

for($i=0; $i < $_SESSION["item_count"]; $i++)
{
if ($_SESSION["items"][$i][0] != $productID)
{
$_SESSION["items"][$pos] = $_SESSION["items"][$i];
$pos++;
}
}
$_SESSION["item_count"]--;
}

function delete_cart()
{
session_destroy();
}
}
?>

<?php
function get_include_contents($filename)
{
if (is_file($filename))
{
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}

$include1 = get_include_contents('includes/topMenu.inc');
$include2 = get_include_contents('includes/sideMenu1.inc');
$include3 = get_include_contents('includes/footer.inc');

?>

<?php
echo "$include1";
echo "<html>";
echo "<head>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/styles.css\"/>";
echo "<meta http-equsiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
echo "<title>Sweet Concepts - Taste The Difference</title>";
echo "</head>";
echo "<body>";
echo "<table>";
echo "<tr>";
echo "<td style=\"vertical-align:top\">";
echo "$include2";
echo "</td>";
echo "<td width=\"520\" style=\"vertical-align:top\">";
echo "<img src=\"images/banners/sampleBasket.gif\"/>";
echo "<table style=\"margin-bottom:80px\">";
echo "<tr>";
echo "<td>";
if (isset($empty))
{
echo $empty;
}
else
{
$basket -> show_cart();
}
echo "</td>";
echo "</tr>";
echo "</table>";

echo "</td>";
echo "</tr>";
echo "</table>";
echo "</body>";
echo "</html>";
echo "$include3";
[/php]








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
• Re: Insert Contents of .txt file into a .html page
• Re: Perl regular expression problem.
• Re: Problems in login using WWW::Mechanize
• searching for gd::graph guide
• Re: ARRAY OF HASHMAPS
• Adv. Regexp or Otherwise
• Adv. Regexp or Otherwise
• I need help pleasee! My project is about using I/o Stream
• Help Running VB script in Windows 2003


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008