Jump to content

How can I access this data?


livepjam

Recommended Posts

[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:
<?php

ini_set( "session.bug_compat_warn", "off" );
require_once('session.php');
require_once('output_page.php');
require_once('nav.php');

?>

<div id="cart">

<table>

<?php

include "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>

<?php

while (!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
Share on other sites

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
Share on other sites

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
Share on other sites

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.