页面加载时删除POST数组

时间:2014-06-23 12:10:12

标签: php post http-post

我有问题的人,我POST数据从form到另一个页面并执行以下与代码相关的事项:

<?php
/*
 * Confirm Registration Page
 * 
*/ 
    $course = $_POST['course_title'];
    $location = $_POST['course_location'];
    $date   = $_POST['course_date'];
    $qty    = $_POST['attendlist'];

    $_POST = array(); <------- Need to clear the POST array so handler is not 
                               called first time user is on page.

    if (!empty($_POST)){
    $processOrder = register_course_handler(); <--- Handler
    }  
?>
<?php get_header(); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <!-- Main Container -->
    <div id="main_box">
        <h1><?= $course . " " . $date ?></h1>
        <h2><?= $location ?></h2>
        <? the_content() ?>
        <!-- Display a form with x amount of fields Name, Email, Number -->
        <form action="#" method="POST">
        <table>
            <th></th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <? for($i = 0; $i < $qty; $i++):?>
            <tr><td>Guest <?= $i + 1?></td>
                <td><input type="text" id="name" name="name"></td>
                <td><input type="text" id="email" name="email"/></td>
                <td><input type="text" id="phone" name="phone"/></td>
                <input type="hidden" name="course_id" value="<?= the_ID() ?>"/>
                <input type="hidden" name="course_title" value="<?= the_title() ?>"/>
                <input type="hidden" name="course_date" value="<?= date("d-m-Y", strtotime(get_sub_field('date'))) ?>"/>
                <input type="hidden" name="course_location" value="<? the_sub_field('venue') ?>"/>
                <input type="hidden" name="course_applicant" value="<?= $user_ID ?>"/>
            </tr>
            <? endfor; ?>
        </table>
        <input type="submit" value="Confirm Registration"/>
        </form>
    </div>
    <?php endwhile; // end of the loop. ?>
    <!-- Shopping Cart Panel (Top Right) -->
<?php include 'order_panel.php'?>
<?php get_footer(); ?>

我希望form重新提交到当前页面。然后我在另一个文件中有一个处理程序来处理提交。目前的问题是处理程序永远不会被调用,因为每次页面加载时都会清除POST数组。我尝试清除它的原因是因为如果我没有,那么第一次将用户带到页面时就会调用处理程序。

有关如何解决此问题的任何建议?

array (size=9)
  'name' => string '' (length=0)     <------- These fields are blank....
  'email' => string '' (length=0)
  'phone' => string '' (length=0)
  'course_id' => string '1063' (length=4)
  'course_title' => string 'Energy use in Mushroom Units' (length=28)
  'formSend2' => string '1' (length=1)
  'course_date' => string '23-07-2014' (length=10)
  'course_location' => string 'Teagasc, Thurles' (length=16)
  'course_applicant' => string '1' (length=1)

我的HTML空白字段。我在浏览器中输入值但空白

<td><input type="text" id="name" name="name"></td>
                <td><input type="text" id="email" name="email"/></td>
                <td><input type="text" id="phone" name="phone"/></td>

1 个答案:

答案 0 :(得分:1)

发送隐藏值:

<input type="hidden" name="formSend" value="1" />

将支票从if (!empty($_POST)){更改为

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

不要$_POST = array();