manuel.koliqi
manuel.koliqi

Reputation: 353

Pass variables to another page PHP

I am trying to pass 2 variables to another page and 1 var is required. I don't know why my code is not working properly. Take a look pls.

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>

Upvotes: 0

Views: 369

Answers (3)

Caio Oliveira
Caio Oliveira

Reputation: 1243

Following your logic, I think your code should be something like this:

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>

Upvotes: 0

Jompper
Jompper

Reputation: 1402

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>

Upvotes: 0

Barmar
Barmar

Reputation: 782437

When the user fills in the required information, you should use a redirect, not another form, to go to active.php. You can pass variables using a session.

<?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
}

Upvotes: 1

Related Questions