Jump to content

Recommended Posts

Hi I am designing a shopping cart. I have stored the cart information within a multiple session array. To display the cart I have used a foreach function. Within this function I have used a remove button to remove individual items. How would I use the $_POST to match the individual item. Please advise.

 

Below is the code I have so far:

 

<?php

session_start();

/**

* @author Grant Kinsman

* @copyright 2009

*/

 

$b = count($_SESSION['cart']);

 

 

if ($_POST[$i]) {

 

$_SESSION['cart'][$i] = "";

$_SESSION['prod'] = $_SESSION['prod']-1;

}

?>

<html>

<head>

<title>Mums Cards</title>

<link rel="stylesheet" href="style.css" style="text/css" />

</head>

 

<body>

<div id="titleBar">

<p class="shopCar"><b>Cart</b><br>

Items - <?php echo $_SESSION['prod']; ?><br>

Total - £<?php echo $_SESSION['total']; ?>

<a href="checkout.php" style="padding-left=0px">Checkout</a></p>

</div>

<div id="menu">

<a href="index.php">Home</a>

<a href="products.php">Products</a>

</div>

<div id="main">

<div id="checkOut">

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<?php

foreach ($_SESSION['cart'] as $name => $id) {

echo "<div class='check'>";

echo "<img src='" .$id['image']. "' class='check2'>";

echo "Order No: " .$id['orderNo']. "<br>";

echo "Quantity: " .$id['quantity']. "<br>";

echo "Unit Price: £" .$id['price']. "<br>";

echo "Info: " .$id['info']. "<br>";

echo "<input type='submit' value='remove' name='" .$i++. "'>";

echo "</div><br>";

}

?></form></div>

</form>

</div>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/163221-solved-php-_post-array/
Share on other sites

I assume you mean something like this

<?php
session_start();
/**
* @author Grant Kinsman
* @copyright 2009
*/

$b = count($_SESSION['cart']);

if ($_POST['remove']) {
   if(isset($_SESSION['cart'][$_POST['Item']]))
   {
	unset($_SESSION['cart'][$_POST['Item']]);
	$_SESSION['prod']--;
   }
}
?>
<html>
<head>
   <title>Mums Cards</title>
   <link rel="stylesheet" href="style.css" style="text/css" />
</head>

<body>
   <div id="titleBar">
      <p class="shopCar"><b>Cart</b><br>
      Items - <?php echo $_SESSION['prod']; ?><br>
      Total - £<?php echo $_SESSION['total']; ?>
      <a href="checkout.php" style="padding-left=0px">Checkout</a></p>
   </div>
   <div id="menu">
      <a href="index.php">Home</a>
      <a href="products.php">Products</a>
   </div>
   <div id="main">
   <div id="checkOut">
   <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
   <?php       
      foreach ($_SESSION['cart'] as $name => $id) {         
         echo "<div class='check'>";
         echo "<img src='" .$id['image']. "' class='check2'>";
         echo "Order No: " .$id['orderNo']. "<br>";
         echo "Quantity: " .$id['quantity']. "<br>";
         echo "Unit Price: £" .$id['price']. "<br>";
         echo "Info: " .$id['info']. "<br>";
         echo "<input type='hidden' value='$name' name='Item'>";
         echo "<input type='submit' value='remove' name='remove'>";
         echo "</div><br>";
      }
   ?></form></div>
   </form>
   </div>
</body>
</html>

your need to put the form in the loop

 

<?php
session_start();
/**
* @author Grant Kinsman
* @copyright 2009
*/

$b = count($_SESSION['cart']);

if ($_POST['remove']) {
   if(isset($_SESSION['cart'][$_POST['Item']]))
   {
      unset($_SESSION['cart'][$_POST['Item']]);
      $_SESSION['prod']--;
   }
}
?>
<html>
<head>
   <title>Mums Cards</title>
   <link rel="stylesheet" href="style.css" style="text/css" />
</head>

<body>
   <div id="titleBar">
      <p class="shopCar"><b>Cart</b><br>
      Items - <?php echo $_SESSION['prod']; ?><br>
      Total - £<?php echo $_SESSION['total']; ?>
      <a href="checkout.php" style="padding-left=0px">Checkout</a></p>
   </div>
   <div id="menu">
      <a href="index.php">Home</a>
      <a href="products.php">Products</a>
   </div>
   <div id="main">
   <div id="checkOut">
   <?php  
      foreach ($_SESSION['cart'] as $name => $id) {         
	echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
         echo "<div class='check'>";
         echo "<img src='" .$id['image']. "' class='check2'>";
         echo "Order No: " .$id['orderNo']. "<br>";
         echo "Quantity: " .$id['quantity']. "<br>";
         echo "Unit Price: &#65505;" .$id['price']. "<br>";
         echo "Info: " .$id['info']. "<br>";
         echo "<input type='hidden' value='$name' name='Item'>";
         echo "<input type='submit' value='remove' name='remove'>";
         echo "</div><br>";
	 echo "</form>";
      }
   ?></div>
   </form>
   </div>
</body>
</html>

 

 

or use a checkbox selection

ie

<?php
session_start();
/**
* @author Grant Kinsman
* @copyright 2009
*/

$b = count($_SESSION['cart']);

if ($_POST['remove']) {
foreach($_POST['Items'] as $Item)
{
   if(isset($_SESSION['cart'][$Item]))
   {
	  unset($_SESSION['cart'][$Item]);
	  $_SESSION['prod']--;
   }
}
}
?>
<html>
<head>
   <title>Mums Cards</title>
   <link rel="stylesheet" href="style.css" style="text/css" />
</head>

<body>
   <div id="titleBar">
      <p class="shopCar"><b>Cart</b><br>
      Items - <?php echo $_SESSION['prod']; ?><br>
      Total - £<?php echo $_SESSION['total']; ?>
      <a href="checkout.php" style="padding-left=0px">Checkout</a></p>
   </div>
   <div id="menu">
      <a href="index.php">Home</a>
      <a href="products.php">Products</a>
   </div>
   <div id="main">
   <div id="checkOut">
   <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
   <?php  
      foreach ($_SESSION['cart'] as $name => $id) {         
         echo "<div class='check'>";
         echo "<img src='" .$id['image']. "' class='check2'>";
         echo "Order No: " .$id['orderNo']. "<br>";
         echo "Quantity: " .$id['quantity']. "<br>";
         echo "Unit Price: &#65505;" .$id['price']. "<br>";
         echo "Info: " .$id['info']. "<br>";
         echo "<input type='checkbox' value='$name' name='Item[]'>";
         echo "</div><br>";
      }
   ?>
<input type='submit' value='remove' name='remove'>
</form></div>
   </form>
   </div>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.