0

我想检查表单的变量。在这里,我希望“a”和“b”是 5 和 99 之间的数字。如果是这样,我想自动重定向到下一页,如果不保留到此页面。

让我通过展示我制作的一段代码来解释一下:(对不起,我花了 15 分钟来理解如何在这里编写代码,但我不明白,所以我会在必要时使用空格)

<input type="text" name="a"><br>
<input type="text" name="b"><br>
<input type="submit" value="GO!">

<?php
if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && 
$_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4) {
  echo "corect";
  //here i want to go to the next page like registration_complete.php
}
else {
  //here i want to remain to this page and show the errors to the user
}
?>

对于这个可能很简单的问题,我很抱歉,但我在谷歌上只找到了愚蠢的答案。

4

5 回答 5

2

First you must check it before you echo something. Then use header("Location: mylogin_page.php");.
What the point to echo something when you have chance that user will not see it? Then use header BEFORE html output.

于 2012-10-26T10:11:41.500 回答
1

Because you are already outputting content to the screen, you cannot use the header('Location: mypage.php'). If you can't or don't want to do this, an alternative would be to use a javascript redirect:

echo "<script>document.location.replace = 'mypage.php';</script>";
于 2012-10-26T10:13:21.243 回答
1

首先,header在将任何 html 代码发送到浏览器后,您将无法使用,因此您的 php 代码应该在另一个文件中,因此您应该:

first-step.php其中包含:

<?php
   session_start();
?>
<html>
   <head>
   </head>
   <body>
   <?php
      if(isset($_SESSSION['error'])) {
         echo $_SESSION['error'];
         unset($_SESSION['error']);
      }
   ?>
   <form action="actions.php?step=1" method="POST">
     <input type="text" name="a"><br>
     <input type="text" name="b"><br>
     <input type="submit" value="GO!">
   </form>
   </body>
</html>

actions.php

<?php
  session_start();

  switch($_GET['step']) {
     case 1:
       if(is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && $_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4)
       {
         header('Location: second-step.php');
         exit;
       }
       else
       {
         $_SESSION['error'] = 'my error';
         header('Location: first-step.php');
       }
     break;
   }
?>
于 2012-10-26T10:17:10.433 回答
0
<?php
    if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && $_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4)
    {
        header('location:correctpage.php');
    }
    else
    {
      foreach($POST as $key=>$post)
     {
       if(empty($post))
      {
        $error [] ="$key is empty required";//do your custom error handling this is just a demo
      }
        print_r($error);
        //here i want to remain to this page and show the errors to the user
    }
于 2012-10-26T10:14:41.177 回答
0
<input type="text" name="a"><br/>
<input type="text" name="b"><br/>    
<input type="submit" value="GO!">    

<?php    
if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && 
$_POST["a"]<100 && $_POST["a"]>4 && $_POST["b"]<100 && $_POST["b"]>4) {
  //echo "corect"; //why 'echo' then redirect?
  echo "<script>";
  echo "top.location = 'registration_complete.php';";
  echo "</script>";
  exit();
}    
else {
  //here i want to remain to this page and show the errors to the user
  //(if you want so, just stay executing this page)
}    
?>
于 2012-10-26T10:15:05.740 回答