Jump to content

Shopping Cart troubles


davemoody

Recommended Posts

G'day,

 

I'm trying to get this shopping cart script to work. the relevent code is:

 

<?php

 

echo  "<pre>" ;

print_r ( $_SESSION );

echo  "</pre>" ;

 

if(isset($_POST['submit']))

{

$itemname = $_POST['h1'];

echo $itemname;

//echo $_SESSION['itemname'][$itemname];

unset($_SESSION['itemqty'][$itemname]);

unset($_SESSION['itemprice'][$itemname]);

unset($_SESSION['itemname'][$itemname]);

}

 

echo "<p />";

echo "<table>";

echo "<tr><th class='a'>Name</th><th class='a'>Quantity</th><th class='a'>Price</th><th class='a'></th></tr>";

foreach($_SESSION['itemname'] as $key=>$value)

{

echo '<tr><td>'.$_SESSION['itemname'][$key].'</td>

<td><input type="text" name="t1" value='.$_SESSION['itemqty'][$key].'></td>

<td>$'.$_SESSION['itemprice'][$key].'</td>

<td><form id="f1" method="post" name="f1"><input type="submit" name="submit" value="Delete Item">

<input type="hidden" name="h1" value='.$key.'></td></tr>';

}

echo "</table>";

 

?>

 

 

 

 

It correctly displays a table, but herein lies the problem. When the last item is added to the table, it's key value becomes the key value for all the buttons. So, if I choose to delete for example the first item in the list, it's actually the last item that gets deleted. I've been staring at this code for about 10 hours and can't get anywhere. Does anyone have any clue what I could do to get it working?

 

If it helps, here's the cart script on each of the product pages:

 

The header:

 

<?php

session_start();

if (isset($_POST['submit'])) {

if(!isset($_SESSION['item'])) $_SESSION['item'] = '';

if($_SESSION['item'] == $_POST['h1']) {

$_SESSION['price'] += $_POST['h2'];

} else {

$_SESSION['item'] = $_POST['h1'];

$_SESSION['price'] = $_POST['h2'];

}

$_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];

$_SESSION['itemqty'][$_SESSION['item']] = ($_SESSION['itemprice'][$_SESSION['item']] / $_POST['h2']) + 1;

$_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];

 

}

?>

 

 

The body:

 

<?php

if(!$_SESSION['username'] == '') {

echo '<form action="products_hillsong_godhe.php" method="post">

<input type="hidden" name="h1" value="New Day" />

<input type="hidden" name="h2" value="19.99" />

<input type="submit" name="submit" value="Add To Shopping Cart" />

</form>';

}

?>

Link to comment
https://forums.phpfreaks.com/topic/244499-shopping-cart-troubles/
Share on other sites

you don't seem to be closing the <form> element.

 

add </form> to the end of this:

 

<form id="f1" method="post" name="f1"><input type="submit" name="submit" value="Delete Item">
                     <input type="hidden" name="h1" value='.$key.'></form>

Thanks for checking it out for me. the contents of the array produced by that test code is as follows:

 

Array

(

    [username] => test

    [item] => A Beautiful Exchange

    [price] => 24.99

    [itemname] => Array

        (

            [Transformation] => Transformation

            [A Beautiful Exchange] => A Beautiful Exchange

        )

 

    [itemqty] => Array

        (

            [Transformation] => 1

            [A Beautiful Exchange] => 1

        )

 

    [itemprice] => Array

        (

            [Transformation] => 9.99

            [A Beautiful Exchange] => 24.99

        )

 

)

man, I just did a little test here, and your problem is what I said the first time: you need to close the form. I used this to test:

 

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
echo '<pre>';
print_r($_POST);
}
for($i=1;$i<10;$i++){
echo '<form id="f1" method="post" name="f1"><input type="submit" name="submit" value="Delete Item">
                     <input type="hidden" name="h1" value='.$i.'></form>';
}
?>

 

without </form> at the end, I always get the number 9, with </form> everything works.

Ok, asssuming this is correct as you say, you're missing the foreach loop which constructs the table from the original data, you've replaced it with a normal for loop. I'm a beginner at this, how do I incorporate your fix so that it still builds that table of data?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.