Jump to content

[SOLVED] PHP $_POST array


sum_guy

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>

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.