Jump to content


Photo

How can I access this data?


  • Please log in to reply
4 replies to this topic

#1 livepjam

livepjam
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 26 September 2006 - 08:02 PM

<INPUT TYPE="text" NAME="name=<?php echo $_SESSION['cart'][$i]; ?>" SIZE="1" value="<?php echo $_SESSION['qty'][$i]; ?>

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:

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');


?>


#2 livepjam

livepjam
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 26 September 2006 - 09:49 PM

Can someone help me with this? Am I going about this incorrectly?

Thanks.

#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 26 September 2006 - 10:33 PM

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... ?

#4 livepjam

livepjam
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 27 September 2006 - 03:09 AM

Nevermind - had another question here, but I want to try something first.

#5 livepjam

livepjam
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 27 September 2006 - 04:50 AM

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:

NAME="<?php echo $_SESSION['cart'][$i]; ?>

I am doing the output in the while loop:

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;

Because I am using a PHP array as the name of each textbox, I am having trouble accessing the data using $_POST.


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

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?  ???




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users