提交表单,进行页内处理,然后再次提交

Submit form, do in-page processing, and submit again

我有两个 PHP 页面,A 和 B。

页面 A 有一个表单,它将值为 'edit' 的 $_POST['mode'] 变量传递给页面 B。

页面B也有它的表单,其提交按钮被命名为'_go'。页面 B 检查是否设置了 $_POST['_go'] 参数并在同一页面 B 中处理表单。

问题是,在页面 B 上,如果我在表单上输入一些值并第一次提交表单,它会起作用并且我会收到成功消息。但是,当我收到成功消息后,没有刷新页面,重新输入另一个值并重新提交表单,我什么也没得到,重新输入的值没有被处理。

我自己调查了一下,发现 $_POST['_mode'] 是问题的原因。当我第一次提交表单时,$_POST['_mode'] 是 'edit'。但是当我重新提交表单时,$_POST['_mode'] 仍然设置但它的值为空。

我在这里很沮丧。

  1. 页面 B 从页面 A
  2. 获取价值 'edit' 的 $_POST['mode']
  3. 页面B将$_POST['mode']存储到变量$mode中,现在$mode的值为'edit'
  4. 页面B提交表单,此时设置了$_POST['_mode'],$_POST['_mode']的值为'edit'.
  5. B页面重新提交表单,因为我没有刷新页面什么的,$mode的值应该还是'edit',我想$_POST['_mode']的值也是 'edit',但是设置了 $_POST['_mode'] 但是 EMPTY。

如何保持 $mode 不变?

我在页面 A 中的代码如下(简化)。

// PAGE A

<form action="Page B" id="some id" name="some name" method="post">
  <input type="hidden" name="mode" value="edit" />
  <input type="submit" class="btn" value="Edit" />
</form>

我在页面 B 中的代码如下(简化)。

// PAGE B

<?php

  // if form is submitted and mode is 'edit', go process the form

  if( isset( $_POST['_go'] && isset( $_POST['_mode'] ) ) {

    $_go_mode = $_POST['_mode'];

    if( $_go_mode == 'edit' ) {
      process_form();
    }

  }

  function process_form() {

    // This function receives parameters from the form
    // and does what it needs to do

    echo '<div class="process-result">';
    echo 'Successfully made changes';
    echo '</div>';

  }

?>

<div>

  <?php

  // This $_POST['mode'] variable is passed from Page A

  if( isset( $_POST['mode'] ) ) {
    $mode = $_POST['mode'];
  }

  ?>

  <form method="post" class="form-horizontal">

    <div class="form-group">
      <label class="control-label">Some label</label>
      <input type="text" class="form-control" name="_name" value="some value" />
    </div>

    <div class="form-group">
      <input type="hidden" name="_mode" value="'.<?php echo $mode; ?>.'" />
      <input type="submit" name="_go" class="btn" value="Make Changes" />
    </div>

  </form>

</div>

作为解决方法,我更改了如下代码。现在它按预期工作,但我觉得这不是一个好方法...

Before:
        if( $_go_mode == 'edit' ) {
          process_form();
        }

After:
        if( $_go_mode == 'edit' ) {
          $_POST['mode'] = $_go_mode;
          process_form();
        }

我已经修改了您的代码以使用会话,它将解决丢失变量的问题,您甚至可以在其他页面中重用它。我已将页面 a 表单的操作设置为“”,因为我在页面 A 中创建会话然后重定向到页面 b。注意 "session_start();" 必须位于每个页面的顶部,它上面的唯一代码可以是“

A​​页

<?php
session_start();
   if( isset( $_POST['mode'] ) ) {
      $mode = $_POST['mode'];
      //setting a session
      $_session['mode']=$mode;
      //put your own file name for b in next line
      header("location:pageb.php");
      }
?

<form action="" id="some id" name="some name" method="post">
  <input type="hidden" name="mode" value="edit" />
  <input type="submit" class="btn" value="Edit" />
</form>

B页

<?php
session_start();
//checking for the session and assigning variable name 
if (isset($_session['mode'])) {
    $mode = $_session['mode'];
    }
// if form is submitted and mode is 'edit', go process the form
if( isset( $_POST['_go'] && isset( $_POST['_mode'] ) ) {
    $_go_mode = $_POST['_mode'];
    if( $_go_mode == 'edit' ) {
       process_form();
       }
  }

  function process_form() {

    // This function receives parameters from the form
    // and does what it needs to do

    echo '<div class="process-result">';
    echo 'Successfully made changes';
    echo '</div>';

  }

?>

<div>


  <form action "" method="post" class="form-horizontal">

    <div class="form-group">
      <label class="control-label">Some label</label>
      <input type="text" class="form-control" name="_name" value="some value" />
    </div>

    <div class="form-group">
      <input type="hidden" name="_mode" value="<?php echo $mode; ?>" />
      <input type="submit" name="_go" class="btn" value="Make Changes" />
    </div>

  </form>

</div>

检查此张贴到同一页面

<html>
    <head>
    </head>
    <body>
        <form name="anyname" action="" method="post">
            <input type="radio" name="choice" value="asia"/>asia
            <input type="radio" name="choice" value="africa"/>africa
            <input type="submit" name="submit" value="my radio choice">
        </form>
<?php 


    function choose(){
        //declare it is a global variable from outside the function
        global $choice;


        if ($choice =='africa') {
        $test='you choose africa';
        }   
        elseif ($choice =='asia'){
        $test='you choose asia';
        }
        //returning the output of the function
        return $test;
        }



    //here you proccess the data after the page reloads

    if (!empty($_POST['choice'])){  
        $choice=$_POST["choice"];
        //running the function
        choose();
        //getting the output from the function
        $testafter=choose();
        echo 'this is the data after the page reloads :',$aftertest;
        }



    //here you process the data before the page reloads
    if ((isset($_POST["submit"])) && ($_POST["submit"]!="")){
        $choice=$_POST["choice"];
        //running the function
        choose();
        //getting the output from the function
        $testbefore=choose();

        }
?>


    <body>
</html>