Jump to content

Forms & Foreach & $_SESSION woes


cyrilsnodgrass

Recommended Posts

I know I am not getting this and I've posted various problems on forum to no avail but I think I now actually understand the problem - hurrah !

 

Consider the following code - what's actually happened is that another form sets up the $_POST['item'] being added to the cart.

Then this page rebuilds the shopping cart display.

 

However, if the user decides to reduce the qty by 1, the only item that gets updated is the last item ever entered into the cart.

 

The problem is something to do with the foreach loop building the form prior to sending it back to the browser.

 

Is this something to do with the foreach construct and references ?

 

What I also don't understand is that once parsed all the values look good but if the user clicks the remove button

$_POST['remitem'] never has the value I expect ??????

 

Someday I will understand - I promise !

 

<code>

 

  $item = $_POST['item'];

 

  $disp_str = "<h2>Added to your cart: ";

  $disp_str .= $item;

  $disp_str .= "</h2><p><form method='post' action='action.php'>";

  $disp_str .= "<table border='1'><tr><td colspan='3'><h2>Your Shopping Cart:</h2></td></tr>";

 

/** Loop to retrieve and build table values from cart                                **/

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

  {

      $cart_item =& $key;

      $disp_str .= "<tr><td><img src='images/";

      $disp_str .= $cart_item;

      $disp_str .= ".jpg' alt='";

      $disp_str .= $cart_item;

      $disp_str .= "'></img></td>";

      $disp_str .= "<td>$cart_item qty = ";

      $disp_str .= "$value </td>";

      $disp_str .= "<td><input type='submit' name='rem' value='remove'</input>";

      $disp_str .= "<input type='text' name='remitem' value='";

      $disp_str .= $cart_item;

      $disp_str .= "'></input><input type='text' name='cartaction' value='remove'</input></td></tr>";

  }

  $disp_str .= "</table></form>";

  echo $disp_str;

 

</code>

Link to comment
https://forums.phpfreaks.com/topic/47519-forms-foreach-_session-woes/
Share on other sites

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.