首页 > 解决方案 > 我会在一页中使用 $ _POST 两次

问题描述

我有一个付款的网站,通过它创建账单,但问题是我不能一次使用 $ _POST 两次,这个例子:

    <?  if (isset($_POST['submit'])) { ?>
        <form action="" method="POST" >
              <input name="invoice" value="" type="text" />
              <input name="pay" value="Pay Now" type="submit" />
        </form>
<? }   if (isset($_POST['pay'])) {
        // MY QUERY HERE
        // HEADERS HERE
} else { ?>
<form action="" method="POST" >
    <input name="info" value="" type="text" />
    <input name="submit" value="Submit" type="submit" />
</form>  
<?  } ?>

标签: php

解决方案


试试这个。

请检查代码以获取评论。

<?
  if (isset($_POST['submit'])) {
    $info = $_POST['info'];
   // use echo to display second form
   echo '
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
      <!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
      <!-- This is to keep the record passed from the first form -->
      <input name="info" value="'. $info .'" type="hidden" />
      <input name="invoice" value="" type="text" />
      <input name="pay" value="Pay Now" type="submit" />
    </form>';
  } else if (isset($_POST['pay'])) {

    // MY QUERY HERE

  } else {
    // Note the action value, it's for post back. This allow you to post back to the current page
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
       <input name="info" value="" type="text" />
       <input name="submit" value="Submit" type="submit" />
    </form>
  }
?>

推荐阅读