livepjam Posted September 26, 2006 Share Posted September 26, 2006 [code]<INPUT TYPE="text" NAME="name=<?php echo $_SESSION['cart'][$i]; ?>" SIZE="1" value="<?php echo $_SESSION['qty'][$i]; ?>[/code]I am trying to build a shopping cart and the code above is the QTY box. This is sitting in a while loop so it outputs for each game in the cart.Lets say I am using a Submit button to POST the update in QTY, how would I access the data above using $_POST?Something like $_POST[$_SESSION['cart']] ?Basically, the text box is going to be equal to the name of the game. So, after I access this data I'm going to use the name from the box to update the quantity in the array that it is being stored in.Here is the full code in case:[code]cart.php:<?phpini_set( "session.bug_compat_warn", "off" );require_once('session.php');require_once('output_page.php');require_once('nav.php');?><div id="cart"><table><?phpinclude "dbgamesconn.php";$name = $_GET['id'];if (!empty($_GET['id'])){$query ="SELECT * FROM games WHERE title='$name'";$result = mysql_query($query);$upc = mysql_result($result,0,"upc");$title = mysql_result($result,0,"title");$platform = mysql_result($result,0,"platform");$desc1 = mysql_result($result,0,"desc1");$desc2 = mysql_result($result,0,"desc2");$pricemem = mysql_result($result,0,"pricemem");$pricenon = mysql_result($result,0,"pricenon");$image = mysql_result($result,0,"image");mysql_close();$m = 0;$_SESSION['cart'][]= $title;$_SESSION['price'][]= $pricenon;if (empty($_POST['qty'])){$_SESSION['qty'][]= '1';}else{while (!empty ($_SESSION['cart'][$m]))://Trying to update quantity array here. I need to access the QTY box after clicking submit. Then I would like to search the $_SESSION['cart] array// and find the game I am looking for. Once I have found the game, I would like to set the corresponding QTY array equal to the updated value in the // text box. $m++;endwhile;}$totalprice = 0.00;}$i = 0;if (empty($_SESSION['cart'][$i])){?><tr><td><?php echo "You have no items in your cart."; ?> </tr></td></table></div><?php}else{?><form method="POST" action="cart.php"><th>Quantity</th><th>Game</th><th>Price</th><?phpwhile (!empty ($_SESSION['cart'][$i])):?><tr><td><INPUT TYPE="text" NAME="qty?name=<?php echo $_SESSION['cart'][$i]; ?>" SIZE="1" value="<?php echo $_SESSION['qty'][$i]; ?>"></td><td><a href="game.php?id=<?php echo $_SESSION['cart'][$i]; ?>"><?php echo $_SESSION['cart'][$i]; ?></a></td><td><?php echo $_SESSION['price'][$i]; ?> </td></tr><?php$totalprice+=$_SESSION['price'][$i] * $_SESSION['qty'][$i]; $i++;endwhile;?><tr><td><b><label>Price: </label></b></td><td><label> </label></td><td><?php echo $totalprice; ?> </td></tr><tr><td><input type="SUBMIT" value="Update Cart" name="Submit"></td></tr></table></form></div><?php}require_once('footer.php');?>[/code] Link to comment https://forums.phpfreaks.com/topic/22149-how-can-i-access-this-data/ Share on other sites More sharing options...
livepjam Posted September 26, 2006 Author Share Posted September 26, 2006 Can someone help me with this? Am I going about this incorrectly?Thanks. Link to comment https://forums.phpfreaks.com/topic/22149-how-can-i-access-this-data/#findComment-99247 Share on other sites More sharing options...
alpine Posted September 26, 2006 Share Posted September 26, 2006 if you are going to access it on the script right after you do submit (form action target), it will be accessable by $var=$_POST['form_fieldname'];or if you are storing everything in a session it is $var=$_SESSION['session_name'];I dont know if this was what you wanted to know... ? Link to comment https://forums.phpfreaks.com/topic/22149-how-can-i-access-this-data/#findComment-99279 Share on other sites More sharing options...
livepjam Posted September 27, 2006 Author Share Posted September 27, 2006 Nevermind - had another question here, but I want to try something first. Link to comment https://forums.phpfreaks.com/topic/22149-how-can-i-access-this-data/#findComment-99378 Share on other sites More sharing options...
livepjam Posted September 27, 2006 Author Share Posted September 27, 2006 Okay, yeah I understand that you would say $_POST['form_fieldname']; to get the data. My issue is this:I am trying to output this page dynamically by using the same textbox, but by giving it a different name each time such as this:[code]NAME="<?php echo $_SESSION['cart'][$i]; ?>[/code]I am doing the output in the while loop:[code]while (!empty ($_SESSION['cart'][$i])):?><tr><td><INPUT TYPE="text" NAME="<?php echo $_SESSION['cart'][$i]; ?> SIZE="1" value="<?php echo $_SESSION['qty'][$i]; ?>"></td><td><a href="game.php?id=<?php echo $_SESSION['cart'][$i]; ?>"><?php echo $_SESSION['cart'][$i]; ?></a></td><td><?php echo $_SESSION['price'][$i]; ?> </td></tr><?php$totalprice+=$_SESSION['price'][$i] * $_SESSION['qty'][$i]; $i++;endwhile;[/code]Because I am using a PHP array as the name of each textbox, I am having trouble accessing the data using $_POST.[code]<tr><td><INPUT TYPE="text" NAME="Game 1" SIZE="1" value="1"></td><td><a href="game.php?id=Game 1">Game 1</a></td><td>49.99 </td></tr><tr><td><INPUT TYPE="text" NAME="Game 2" SIZE="1" value="1"></td><td><a href="game.php?id=Game 2">Game 2</a></td><td>55.99 </td></tr>[/code]If I had two items in my cart, the resulting HTML is above.Because I am using an array to store the name of the game and assigning the text box the name of the game, how can I access the Post data using the name of the array?Does that make sense? ??? Link to comment https://forums.phpfreaks.com/topic/22149-how-can-i-access-this-data/#findComment-99411 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.