I have a question about carrying an entire html table from one page to another in a PHP variable.

I would have thought that I could do it, but it doesn't seem to want to work

It is a shopping cart displayed in a table, and when they accept the cart, I want to carry it to the next page to be used in the body of an email that will be sent out. I know I could rebuild it for the email, but why do it twice if there is a way to reuse the first build.

Here is what I have:

$cart_display="
<table align='center' width='900' border='1' bordercolor='#000066' cellpadding='3' cellspacing='0'>
  <tr>
    <td align='left' width='20%'>&nbsp;</td>
    <td align='center' colspan='3'><h3 class='B'><font color='#3366FF'>Dealer Product Purchase Order</font></h3></td>
    <td align='right' width='20%' class='bold12'>Order ID<br>".$del_id."_".$cart_id."</td>
  </tr>

  <tr>
    <td align='left' colspan='2' class='bold12'>BILL TO : <br><br>".$bill_to_block."</td>
    <td align='left' colspan='3' class='bold12'>SHIP TO : ";

    if ($deliver_to=='C'){
      $cart_display.="Customer";
    }elseif ($deliver_to=='I'){
      $cart_display.="Installer";
    }else{
      $cart_display.="Dealer";
    }

    $cart_display.="
    <br><br>".$ship_to_block."</td>
  </tr>
  <tr>
    <td align='center' width='20%'>Product ID</td>
    <td align='center' width='30%'>Product Name</td>
    <td align='center' width='15%'>Price</td>
    <td align='center' width='15%'>Quantity</td>
    <td align='right' width='20%'>Line Total</td>
  </tr>
";

$sql = "
  SELECT item_id, product_id, qty, price_each
  FROM product_item
  WHERE cart_id='$cart_id'
  AND status='A'
  ORDER BY prod_type desc
";
  $result=mysql_query($sql);
  // iterate through each record for any special processing required
  while($cart_item=mysql_fetch_array($result)){
    //  secondary query for product info
    $sql2 = "
      SELECT product_name
      FROM dealer_product
      WHERE product_id='".$cart_item[1]."'
      AND status='A'
    ";
    $result2=mysql_query($sql2);
    $cart_product=mysql_fetch_array($result2);
      // add line item to table
    $cart_display.="
      <tr>
        <td align='center'>".$cart_item[1]."</td>
        <td align='left'>".$cart_product[0]."</td>
        <td align='center'>$".number_format($cart_item[3], 2)."</td>
        <td align='center'>".$cart_item[2]."</td>
        <td align='right'>$".number_format(($cart_item[2]*$cart_item[3]), 2)."</td>
      </tr>
    ";
  }// ends  while($cart_item=mysql_fetch_array($result))

// ###  finished dealing with individual items - now deal with cart as a whole

  //  Need a total line
  $cart_display.="
    <tr>
      <td align='left'>Date Ordered : </td>
      <td align='left' colspan='2'>".$created."</td>
      <td align='right'>Total </td>
      <td align='right'>".$sub_total."</td>
    </tr>
  </table>
";//  End of building cart display


print"<br><br>";
print $cart_display;
 print "<br><br></td></tr>";
 // 2 links - 1 for Return to Cart and 1 for Submit Purchase Order
?>
<tr>
  <td align='center'>
      <form name="shop" action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
      <input type="image" name="shop" src="images/cart_view.png" width="220" height="50" border="0" alt="Continue Shopping">
      </form>
  </td>
  <td align='center'>
      <form name="shop" action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
      <input type='hidden' name='sub_act' value='submit_order'>
      <input type='hidden' name='cart_display' value='$cart_display'>
      <input type="image" name="submit" src="images/button_submit_blue.png" width="220" height="50" border="0" alt="Submit Purchase Order">
      </form>
  </td>
 </tr>

But what I get is when it is supposed to display the submit button image, instead, it displays the entire shopping cart

A little baffled, but maybe I'm trying to do something that I can't do.

Any guidance with this would be greatly appreciated.

Douglas

Recommended Answers

All 3 Replies

The main problem with that would probably be that you are dumping the entire $cart_display string unescaped into a HTML value attribute. That won't work; the HTML in the $cart_display will corrupt the HTML page it's being dumped into. To make this work, you'd have to pass it through a function like htmlentities() first, to make it a valid value that won't corrupt the HTML it's being placed in.

With that said, I think you are over complicating the situation. I would suggest abandoning this approach and just rebuilding the table for the email after submission. I can't say for sure which approach performs better, but either way the improvement is negligable. This relative non-issue is not worth jumping through these kinds of hoops.

However, if you are determined to cache the table and reuse it, a better approach may be to cache it on the server, and only pass an identifier through the HTML form. Dump the pre-built HTML table into a session variable or a file on the server side, and then fetch it from there when it comes time to send the email. - What you are doing now is sending the whole thing to the client, only to have to read it all back to the server when the form is submitted. That's not a great move, performance vise.

Thank you for your response. I think that was exactly the answer I was looking for.

Trying to figure out if it was a smart way to do it (if I had been approaching it correctly), or if there was a better way... I guess it would be quicker and easier to just rebuild it. I can just put that chunk of code into a separate file and include it, so if I make any changes in the future, it will remain the same for both uses withough having to modify the code twice.

Thanks again.
Douglas

WOW... glad I asked the question in here, because I always seem to get the answers I need.

Why I didn't think of that before, I have no clue...

It worked perfectly, both for the display and for the following page in the email body

And it took less than 15 minutes to accomplish.

Thank You Again...

Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.