Php 如果使用$\u POST检查按钮状态,则使用空变量发送邮件

Php 如果使用$\u POST检查按钮状态,则使用空变量发送邮件,php,forms,email,variables,post,Php,Forms,Email,Variables,Post,我有一个简单的表单,可以从index.php页面上的一些项目(食物)中进行选择。此页面通过POST将信息发送到“order.php”。在“order.php”页面上,我可以回显上一页上选择的项目,但是如果我尝试转发它们,就会收到一封空邮件 如果我使用另一个If条件“($x>0)”,我就可以通过邮件获得竞争。“如果(设置($_POST['confirm'])”有什么错 非常感谢您的任何建议 <!DOCTYPE html> <head> <meta http-equiv

我有一个简单的表单,可以从index.php页面上的一些项目(食物)中进行选择。此页面通过POST将信息发送到“order.php”。在“order.php”页面上,我可以回显上一页上选择的项目,但是如果我尝试转发它们,就会收到一封空邮件

如果我使用另一个If条件“($x>0)”,我就可以通过邮件获得竞争。“如果(设置($_POST['confirm'])”有什么错

非常感谢您的任何建议

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

点菜
您的订单如下:

由于您正在构建一个两阶段表单,因此需要记住,PHP在来自浏览器的HTTP请求之间本质上是无状态的。除非将值显式存储在
$\u SESSION
中供以后使用,否则这些值将不会出现在后续页面加载中。单击
确认
将计为后续页面加载

因此,最简单的解决方案是将构造为
$items
的字符串存储到
$\u SESSION
中,并在处理确认时从中读取

这不需要更改初始表单。但是,您需要对
order.php
进行一些修改

接下来的三个部分应该或多或少地取代了问题顶部原始
order.php
中的代码

首先,初始化
会话\u start()

好的,现在当用户单击
confirm
时,您的值将保持不变。让我们在最后一节中处理这个问题:

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



您没有要包含的标题,
$headers
目前只是一个(空)变量,但尚未为其设置/分配任何内容。我还想把
if(isset($\u POST['confirm']){
放在
$items=''上面;
现在的方式,由于条件语句的位置,没有其他东西被使用。它之外的所有东西(
if(isset($\u POST['confirm'])
)都被忽略。@Fred ii-添加了“$headers=“xxx”;”现在得到了“xxx”我打开了我的收件箱,但“消息”仍然是空的。如果我回显条件中的“$items”也是空的。我做错了什么?:(请慢慢仔细地重新阅读我的评论。@Fred ii-Ops…对…通读…@Fred ii-我修改了代码,但“消息”仍然是空的。请查看上面更新的代码(在原始帖子底部)…谢谢:(Michael,在
$\u SESSION['itmems'])
=>
$\u SESSION['items'])
@Fred ii-刚刚注意到你在评论,谢谢,还有另一个问题是仍然阅读
$items
而不是
$\u SESSION['items']
,不客气。(我提到它只是为了以防万一OP马上复制它。).另一件事Michael.我测试了你的答案,它奏效了,但那只是在我添加了
session\u start()之后
$items='';上面的
$items='';
缺少了哪些;我想我会让你知道的。除此之外,它工作得很好。除非这三个代码体一起使用?如果是这样,它将抛出一个解析错误,因为
$\u会话['items]后最后两个大括号下缺少
?>
=$items;
@Fred ii-在
之前,我确实有
会话_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>

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("foo@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>
<?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....
<?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;
  }
}
<!-- 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
}
?>