0

フォームの変数を確認したい。ここでは、"a" と "b" を 5 から 99 までの数字にしたいと考えています。それが true の場合、このページに留まらない場合は、次のページに自動的にリダイレクトします。

私が作成したコードを示して説明させてください: (申し訳ありませんが、ここでコードを記述する方法を理解するのに 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
}
?>

このおそらく単純な質問で申し訳ありませんが、Googleでばかげた答えしか見つかりません。

4

5 に答える 5

2

まず、何かをエコーする前に確認する必要があります。次に、を使用しますheader("Location: mylogin_page.php");
ユーザーに表示されない可能性がある場合に、何かをエコーするポイントは何ですか?次に、HTML出力の前にヘッダーを使用します。

于 2012-10-26T10:11:41.500 に答える
1

既にコンテンツを画面に出力しているため、header('Location: mypage.php'). これができない、またはしたくない場合は、代わりに JavaScript リダイレクトを使用することもできます。

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 に答える