user3150191
user3150191

Reputation: 415

Displaying data using $_POST

I have working code, but i'm have a question about sending data through $_POST that is formatted in a table.

On PAGE1.php, I have this code:

$printoutput .="<tr><td width='10%'> Item #". ($i+1) ."</td></tr> 
<tr width='20%'><td>  " . $product_name . " --&nbsp;$" . $price . ".00</td></tr>
<tr><td width='40%'>" . $displayoptions . "</td>
<tr><td>&nbsp;</td></tr>";

It is submitted through a form, and I use POST to grab the data. On PAGE2.php, when I pull the data through and echo it out, the format is changed completely. In other words, on PAGE1.php the data looks like this:

Item #1
Hamburger -- $6.00
Fruit Cup

Item #2
Pan Roasted Salmon -- $18.00
Vegtables

and on the second page, the data looks like this:

Item #1 Hamburger -- $6.00 Fruit Cup

Item #2 Pan Roasted Salmon -- $18.00 Vegtables

Does anyone know how I can send over the data with the correct formatting? So when it gets to PAGE2.php, I can simply echo out the variable of data and have it displayed the same as on PAGE1.php

I've tried multiple ways to get it to work, but none of it has been correct. Any help would be appreciated. Thank you.

PAGE2.php code looks like this:

if(isset($_POST['order'])) {
  $order = $_POST['order'];
}

echo $order;

Upvotes: 1

Views: 94

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

Edit

Alternate way, a chained method which will produce the same output as my original answer:

Quick Note:

This:

$_SESSION['prodname'] = $product_name = "Product name";

Could be changed to:

$_SESSION['prodname'] = $product_name = $_POST['product_name'];

Taking the information from your POST variable(s). The above were used as a simple test.

Page 1

<?php
session_start();
$_SESSION['prodname'] = $product_name = "Product name";
$_SESSION['prodcost'] = $price = "10";
$_SESSION['display'] = $displayoptions = "Display options";

// testing purposes only
// $price = $_SESSION['prodcost'];
// $displayoptions = $_SESSION['display'];

$printoutput .="<table><tr><td width='10%'> Item #". ($i+1) ."</td></tr> 
<tr width='20%'><td>  " . $_SESSION['prodname'] . " --&nbsp;$" . $_SESSION['prodcost'] . ".00</td></tr>
<tr><td width='40%'>" . $_SESSION['display'] . "</td>
<tr><td>&nbsp;</td></tr></table>";

// echo $printoutput;
$_SESSION['order'] = $printoutput;
echo $_SESSION['order']; // will echo on screen

Page 2

<?php
session_start();
if(isset($_POST['order'])) {
// $order = $_POST['order'];

$_SESSION['order'] = $printoutput;

$printoutput .="<table><tr><td width='10%'> Item #". ($i+1) ."</td></tr> 
<tr width='20%'><td>  " . $_SESSION['prodname'] . " --&nbsp;$" . $_SESSION['prodcost'] . ".00</td></tr>
<tr><td width='40%'>" . $_SESSION['display'] . "</td>
<tr><td>&nbsp;</td></tr></table>";

echo $printoutput; // Successfully printed out data from page 1
}

// You can place the mail() function below this

Original answer

You could use sessions for this.

Being limited with unshown code, have come up with the following:

N.B.: I added <table> and </table> tags as my own test.

Page 1

<?php
session_start();
$product_name = "Product name";
$price = "10";
$displayoptions = "Display options";

$printoutput .="<table><tr><td width='10%'> Item #". ($i+1) ."</td></tr> 
                            <tr width='20%'><td>  " . $product_name . " --&nbsp;$" . $price . ".00</td></tr>
                            <tr><td width='40%'>" . $displayoptions . "</td>
                            <tr><td>&nbsp;</td></tr></table>";

$_SESSION['order'] = $printoutput;
echo $_SESSION['order'];

Page 2

N.B.: echo $_SESSION['order']; may need to be moved inside your conditional statement, yet the following works in the position it's in now.

<?php
session_start();
if(isset($_POST['order'])) {
  $order = $_POST['order'];
}

echo $_SESSION['order'];

which will print:

Item #1
Product name -- $10.00
Display options

The HTML source produced:

<table><tr><td width='10%'> Item #1</td></tr> 
<tr width='20%'><td>  Product name --&nbsp;$10.00</td></tr>
<tr><td width='40%'>Display options</td>
<tr><td>&nbsp;</td></tr></table>

Upvotes: 1

Related Questions