0

I'm working on a script for forms that submits the data to itself to check for errors, if there are no errors it uses header() to send them to the next page. Is there a way I can send the $_POST data to the next page as well without the form being re-submitted?

    <?php

if (isset($_POST['submit'])) {
    $errors = array();
    $moo = array();

    if (empty($_POST['a'])) {
        $errors[] = 'You forgot to answer Question 1!<br/>';
    } else {
        $moo = $_POST['a'];
    }

    if (empty($_POST['b'])) {
        $errors[] = 'You forgot to answer Question 2!<br/>';
    }

    if (empty($_POST['c'])) {
        $errors[] = 'You forgot to answer Question 3!<br/>';
    }

    if (empty($errors)) {

        $_POST['aids'] = "RAWR";
        $url = "http://localhost/test/test.php?page=2";
        header("Location: $url");
        exit();
    } else {
        foreach ($errors as $error) {
            echo $error;
        }
    }
}

echo "
    <form action=\"test.php?page=1\" method=\"post\">
        <input type=\"text\" name=\"a\" value=\"" . $_POST['a'] . "\">
        <input type=\"text\" name=\"b\" value=\"" . $_POST['b'] . "\">
        <input type=\"text\" name=\"c\" value=\"" . $_POST['c'] . "\">
        <input type=\"submit\" name=\"submit\">
    </form>
";

?>
4

3 回答 3

0

您应该使用 AJAX 请求来检查错误,如果所有信息都正确,则将表单提交到 $url 的位置。

但是,如果您想在没有错误的情况下将数据存储在 $_SESSION 变量中,您将能够在下一页检索它。

...
if (empty($errors)) {

    $_POST['aids'] = "RAWR";
    $url = "http://localhost/test/test.php?page=2";
    $_SESSION['post'] = $_POST;
    header("Location: $url");
    exit();
} else {
    foreach ($errors as $error) {
        echo $error;
    }
}
...

甚至可以使用 url 的查询字符串发送数据,例如

$url = "http://localhost/test/test.php?page=2&data=whatever";
于 2013-03-09T00:41:18.720 回答
0

那么有很多方法可以解决这个问题。首先想到的是将数据存储在$_SESSION变量中,然后将其存储在数据库中......

如果您不想过多地更改脚本的结构,而不是在验证数据后进行重定向,只需include()运行其他页面的代码 -

if (empty($errors)) {
  ...
  // this will simply insert the contents of the script and execute it.
  include('test.php?page=2');
} else {
   ...
}
于 2013-03-09T00:44:46.813 回答
-1

尝试不使用任何内容作为操作 url。这使当前页面成为目标:

<form action="" method="post">

编辑。误读问题。在第一页试试这个:

<?php  
  if(isset($_POST['submit'])){
    // check for errors here...
    if (empty($errors)) {
      $valuea = 'a';
      $valueb = 'b';
      $url = "http://domain/test/test.php?vala=".$valuea."&valb=".$valueb."";
      header("Location: $url");
      exit();
    }
  }       
?>

在第 2 页

<?php
$valuea = $_GET['vala'];
$valueb = $_GET['valb'];
?>

这只是通过 url 传递值。否则,您将需要一个会话或数据库来使用 PHP 传递值。

于 2013-03-09T00:40:35.890 回答