将变量传递给另一个页面PHP

时间:2013-12-29 01:55:28

标签: php parameter-passing

我正在尝试将2个变量传递给另一个页面,并且需要1个var。我不知道为什么我的代码不能正常工作。看看吧。

Prova1.php:

 <html>
 <body>
 <?php
         echo htmlspecialchars($_SERVER["PHP_SELF"]);
         $nameErr="";
         $name="";

     $url="Prova1.php";

        if ($_SERVER["REQUEST_METHOD"] == "POST"){

    if (empty($_POST["name"])){
        $nameErr = "Name is Required";
    }else{
    $url="active.php";
         }
   }  
     ?> 
      <form method="post" action="<?php echo $url ?>" >
      Name: <input type="text" name="name"/>* <?php echo $nameErr;?>
     </br>
       Email: <input type="text" name="email"/></br>
       <input type="submit"/>
      </form>

     </body>
     </html>

active.php:

       <html>
       <body>

           Welcome <?php echo $_POST["name"]; ?> </br>
           Your email is <?php echo $_POST["email"]; ?>
       </body>
       </html>

3 个答案:

答案 0 :(得分:1)

当用户填写所需信息时,您应使用重定向而非其他表单转到active.php。您可以使用会话传递变量。

<?php
session_start();

if (isset($_POST['name']) && !empty($_POST['name']) {
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['email'] = $_POST['email'];
    header("Location: active.php");
} else {
    $nameErr = '';
    if (isset($_INPUT['submit'])) {
        $nameErr = 'Name is required'];
    }
    ?>
    <html>
    <body>
    <form method="post" action="" >
        Name: <input type="text" name="name" required/>* <?php echo $nameErr;?>
        </br>
        Email: <input type="text" name="email"/></br>
        <input type="submit" name="submit"/>
    </form>
    </body>
    </html>
    <?php
}

答案 1 :(得分:0)

按照你的逻辑,我认为你的代码应该是这样的:

prova1.php

<html>
    <style>
        .error {color: #FF0000;}
    </style>
    <body>
        <?php

        echo htmlspecialchars($_SERVER["PHP_SELF"]);
        $nameErr = "";
        $name = "";

        $url = "active.php";

        if(isset($_GET['error'])){
          if($_GET['error'] == 1){ //code for name error
            $nameErr = "name is required";
          }
        }

        ?> 
        <form method="post" action="<?php echo $url ?>" >
            Name: <input type="text" name="name"/>* <?php echo $nameErr; ?>
            </br>
            Email: <input type="text" name="email"/></br>
            <input type="submit"/>
        </form>

    </body>
</html>

active.php

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        header("Location: prova1.php?error=1");
    }
}else{
    header("Location: prova1.php");
}
?>
<html>
    <body>
        Welcome <?php echo $_POST["name"]; ?> </br>
        Your email is <?php echo $_POST["email"]; ?>
    </body>
</html>

答案 2 :(得分:0)

<强> proval.php

if(!isset($_POST['name']) || empty($_POST['name'])){
  include 'form.php';
}else{
  include 'active.php';
}

<强> form.php的

<html>
<body><?php
     echo htmlspecialchars($_SERVER["PHP_SELF"]);
     $nameErr="";
     $name="";
     if (isset($_POST["name"])){
       $nameErr = "Name is Required";
     }
 ?> 
  <form method="post">
  Name: <input type="text" name="name"/>* <?php echo $nameErr;?>
 </br>
   Email: <input type="text" name="email"/></br>
   <input type="submit"/>
  </form>

 </body>
 </html>

<强> active.php

   <html>
   <body>
       Welcome <?php echo $_POST["name"]; ?> </br>
       Your email is <?php echo $_POST["email"]; ?>
   </body>
   </html>