Skip to content
Advertisement

I can’t pass a variable through the include statement

I’m just starting with PHP and I’m doing a sign-up form that passes the information to the register.php file and then the script in that file checks if the password field value equals the confirm password field value field then if they aren’t equal, it sets a certain value to the $message variable and then redirects to the index.php file and where that $messege variable should be echoed in a p tag but isn’t printed then

<div class="container">
  <h1>Sign Up</h1>
  <p>
    It's free and only takes a minute!
  </p>
  <div>
    <p class="error">
      <!--here is the $messege variable -->
      <?php
          include 'register.php';
          echo $messege;
          ?>
    </p>
  </div>
  <form class="form" action="register.php" method="post">
    <label>First Name:</label>
    <input type="text" name="first_name"><br>
    <label>Last Name:</label>
    <input type="text" name="last_name"><br>
    <label>Email:</label>
    <input type="email" name="email"><br>
    <label>Password:</label>
    <input type="password" name="password"><br>
    <label>Confirm Password:</label>
    <input type="password" name="confirm_password"><br>
    <input type="submit" name="submit" value="Sign Up!"><br>
    <input type="checkbox" name="terms">
    <span>By clicking the sign up button you
                agree to our <a href="terms.html">Terms & Conditions</a> and <a href="terms.html">Privacy policy</a> </span>
  </form>
</div>

and this the register.php file

<?php

if (isset($_POST['submit'])) {
  $first_name = $_POST['first_name'];
  $last_name  = $_POST['last_name'];
  $from_email = $_POST['email'];
  $password = $_POST['password'];
  $confirm_password = $_POST['confirm_password'];
  if ($password == $confirm_password) {
    $messege = null;
      header("Location: index.php?Succesful");
  } else {
    $messege = "Password and Confirm-Password doesn't match";
    header("Location: index.php?error-in-password");
  }

}

?>

Advertisement

Answer

because in the register.php you first check

isset($_POST['submit'])

but there is not post request and only you have access to $_GET[‘error-in-password’]

so for the echo the message in page you need to

header("Location: index.php?error-in-password&message=$message");

and in your register.php you need to

if (isset($_GET['error-in-password']))
{
    echo $_GET['message'];
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement