Click to See Complete Forum and Search --> : displaying posted data in array AND posting a variable value


meme
03-25-2007, 06:28 AM
Hi,

I am writing a simple storefront pages but I am facing 2 problems I hope you can help me with as my deadline is today!

1) I am collecting data from a table in a form, using a while loop (based on user selection. when I try to display the posted data in the following page. I get the same data repeated 6 times.

2) I do not know how to transfer a variable value into the next page.

Here is my code:

page1:
$counter = 1;

while ($i = mysql_fetch_assoc($results)){

echo "<tr><td>".$i["product_name"]."</td>";
//hidden field to hold the product name
echo "<input type=hidden name='prodname".$counter."' value=".$i["product_name"]." >";

echo "<td>£".$i["product_price"]."</td>";

//hidden field to hold the product price
echo "<input type=hidden name='prodprice".$counter."' value=".$i["product_price"]." >";

/hidden field to hold the product code
echo "<input type=hidden name='prodcode".$counter."' value=".$i["product_code"]." >";


$counter++;
}


if ($_POST[CD] && $_POST[Book]){

$pnp=6;
$total = 45.98;

echo "<tr><td><b>Postage and Packaging</b></td><td>£6.00</td></tr>";
echo "<tr><td><b>Total</b></td><td>£45.98</td></tr>";


}


else {

$pnp=4;
$total = 23.99;

echo "<tr><td><b>Postage and Packaging</b></td><td>£4.00</td></tr>";
echo "<tr><td><b>Total</b></td><td>£23.99</td></tr>";


}


echo "</table>";

echo "<table align = center><tr><td><input type='reset' name='reset' value='Change your selection'></td><td width=20%>&nbsp;</td><td><input type='submit' name='submit' value='Proceed to checkout'></td></tr></table></form>";

}

?>

The second page code:

if( isset( $_POST['submit'] ) )
{

foreach ($_POST as $key => $value) {
//For debugging - prints every iteration of the form posted data
echo "<pre>" . print_r($_POST, 1) . "</pre>";
echo "$total";

}

}
This prints the same data 6 times; like here:

Array
(
[prodname1] => Book
[prodprice1] => 19.99
[prodcode1] => 1
[prodname2] => CD
[prodprice2] => 19.99
[prodcode2] => 2
[submit] => Proceed to checkout
)

Array
(
[prodname1] => Book
[prodprice1] => 19.99
[prodcode1] => 1
[prodname2] => CD
[prodprice2] => 19.99
[prodcode2] => 2
[submit] => Proceed to checkout
)

AND SO ON..............


ALSO I am not sure how to transfer the $total value to the next page.

Many thanks.

meme
03-25-2007, 06:51 AM
Problem solved. After a few hours working on this without success and after deciding to email the forum I found the solution.

I removed the loop foreach ($_POST as $key => $value)
and I posted the value of $total as a hidden field

Many thanks for reading my post :)

NightShift58
03-25-2007, 06:58 AM
Just read your post as it bumped itself up to the top.

For future reference, you should have looped through the post this way:foreach ($_POST as $key => $value) {
//For debugging - prints every iteration of the form posted data
echo "<pre>";
print_r($_POST[$key]);
echo "</pre>";
}

meme
03-25-2007, 01:31 PM
Hi,

I am sorry but I still need a way to display data posted from a form.

The form can contain one order line or 2 order lines.

In the following page I need to display each of these items and its price, etc and capture the values to insert them in a table.

Using the following code does not work.


if( isset( $_POST['submit'] ) )
{

foreach ($_POST as $key => $value) {
//For debugging - prints every iteration of the form posted data
echo "<pre>" . print_r($_POST, 1) . "</pre>";



echo "<p>The prod is <b>".$_POST['prodname'.$key]."</b>";
echo "<p>The price is <b>".$_POST['prodprice']."</b>";



}

}

the only line that works is

echo "<pre>" . print_r($_POST, 1) . "</pre>";

It prints the correct information. For example when selecting 2 items in the previous page it displays correctly everything that was posted from the form:

Array
(
[prodname1] => Book
[prodprice1] => 19.99
[prodcode1] => 1
[prodname2] => CD
[prodprice2] => 19.99
[prodcode2] => 2
[thetotal] => 45.98
[submit] => Proceed to checkout
)

However, how do I capture this data?

here is how I am collecting the form data in the previous page.

$counter = 1;


while ($i = mysql_fetch_assoc($results)){

echo "<tr><td>".$i["product_name"]."</td>";


//hidden field to hold the product name
echo "<input type=hidden name='prodname".$counter."' value=".$i["product_name"]." >";

echo "<td>£".$i["product_price"]."</td>";

//hidden field to hold the product price
echo "<input type=hidden name='prodprice".$counter."' value=".$i["product_price"]." >";

//hidden field to hold the product price
echo "<input type=hidden name='prodcode".$counter."' value=".$i["product_code"]." >";


// echo "<td><input type='checkbox' name ='remove".$counter."' value = 'remove'></td></tr>";
// $addtocart = "insert into vstore_shoopertrack values ('', $PHPSESSID, '$_GET[product_code], now())";

$counter++;
}

If you would like to see the rest of the relevant code for the 2 pages, see my first email in this thread

NightShift58
03-25-2007, 07:35 PM
Me thinks you need to re-read my answer...