Edmond Tamas Edmond Tamas - 1 year ago 35
PHP Question

Mail with empty variables if using $_POST to check for button state

I have a simple form for selecting from some items (food) on my index.php page. This page sends the info to "order.php" by POST. On the "order.php" page I can echo the items picked on the previous page, but If I try to mail them forward I get an empty mail.

If I use another if condition "($x > 0)", I get the contend by mail just fine. What is wrong with: "if (isset($_POST['confirm']))" ??

Any suggestion is highly appreciated!!

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/template.css" type="text/css" rel="stylesheet" />
<title>Ordered Food</title>
</head>
<body>

Your order the following:

<table id="order_table">
<tbody>

<?php
$items = '';
$x = 1;

foreach($_POST as $key => $value){
if ($value == 0) {
continue;
}
$items .= "$key: $value\n";
echo "<tr><td>$key</td><td class='value'>$value</td></tr>";
}

// if( $x > 0 ) this was working
if (isset($_POST['confirm'])) {
$message = $items;
mail("v***@yahoo.com", $subject, $message, $headers);
echo "<p>Thanks for your order!</p>";
}
// }
?>

</tbody>
</table>

<p>
<form method="post">
<input name="confirm" type="submit" value="Send Order">
</form>
</p>

</body>
</html>


The previous page, in case if it is needed.

<!DOCTYPE html>
<head>
<title>Alm Chalet Menu</title>
<link href="css/template.css" type="text/css" rel="stylesheet" />
<script src="js/jquery-1.11.0.js"></script>
<script src="js/template.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<div class="wrapper">
<div class="logo"><img src="images/logo.png"/></div>

<div class="intro_text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</div>

<div class="category">
<p class="categ_title">Biologische Säfte</p>
<div class="form_style">

<form method="post" action="order.php">
<table>
<tr>
<td>
<input type="number" name="orange" min="0" value="0" class="count_style" id="qty">
<span>Orange</span>
</td>
<td>
<div class="field"><input type="number" name="multivitamine" min="0" value="0" class="count_style">Multivitamine</div>
</td>
</tr>
<tr>
<td>
<input type="number" name="grapefruit" min="0" value="0" class="count_style" id="qty">
<span>Grapefruit</span>
</td>
</table>
<input type="submit" name="submit" value="Send Menu" />
</form>
</div>
</div>



</div>
</body>
</html>


===========

Updated code part:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/template.css" type="text/css" rel="stylesheet" />
<title>Ordered Food</title>
</head>
<body>

Your order the following:

<table id="order_table">
<tbody>

<?php

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

$items = '';


foreach($_POST as $key => $value){
if ($value == 0) {
continue;
}
$items .= "$key: $value\n";
echo "<tr><td>$key</td><td class='value'>$value</td></tr>";
}

$headers ="xxx";
$message = $items;
mail("[email protected]", $subject, $message, $headers);
echo "<p>Thanks for your order!</p>";
}
?>

</tbody>
</table>

<p>
<form method="post">
<input name="confirm" type="submit" value="Send Order">
</form>
</p>

</body>
</html>

Answer Source

Since you are building a two-stage form, you need to keep in mind that PHP is essentially stateless between HTTP requests from the browser. Unless you explicitly store values in $_SESSION for later use, they will not be present on a subsequent page load. Clicking confirm counts as a subsequent page load.

So the easiest solution is to simply store the string you constructed as $items into $_SESSION and read from it when processing the confirmation.

This requires no changes to your initial form. However, you'll need a few modifications on order.php.

The three sections that follow should more or less replace the code you had in your original order.php at the top of your question.

First, initialize session_start():

<?php
// At the *top* of order.php, after <?php but before *anything else*
// It must come before the HTML output...
session_start();
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/template.css" type="text/css" rel="stylesheet" />
<title>Ordered Food</title>
</head>
<body>
// etc....

Then when you receive the initial form submission, check for the presence of $_POST['submit'] and store the received key/value pairs for later.

<?php
$items = '';
$x = 1;

// Only if this was the original submission ($_POST['submit'] is set)
if (isset($_POST['submit'])) {    
  foreach($_POST as $key => $value){ 
      if ($value == 0) {
          continue;   
      }
      $items .= "$key: $value\n";
      echo "<tr><td>$key</td><td class='value'>$value</td></tr>";

      // Save the $items string into $_SESSION
      // There are other, possibly better, ways to handle this, like storing the actual
      // $_POST array or a subset, but this is simplest given your existing code
      $_SESSION['items'] = $items;
  }
}

Okay, now your values will persist when the user clicks confirm. Let's handle that in the last section to change:

<!-- close the HTML table before doing this, not after as in your original... -->
</tbody>
</table>

<?php
// If confirm was clicked *and* the items are stored in $_SESSION
// send the email
if (isset($_POST['confirm']) && isset($_SESSION['items'])) {
  // Instead of $items, here read from the session value $_SESSION['items']
  $message = $_SESSION['items'];
  mail("v***@yahoo.com", $subject, $message, $headers); 
  echo "<p>Thanks for your order!</p>";

  // Then unset the value from $_SESSION
  unset($_SESSION['items']);
}
// But only display the confirmation form if confirm wasn't clicked yet!
// Otherwise they'll see the form again after confirmation, which is untidy
else {
?>
<p>
<form method="post">
<input name="confirm" type="submit" value="Send Order">
</form>
</p>
<?php
}
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download