Linked Questions

Popular Questions

Print a message from one page to another in PHP

Asked by At

I am developing a website using PHP. I have now two files (two pages) sign_in.php and register.php. In register.php I have ONLY the html code for the register form.

<?php
require_once 'core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
?>

<div class="container-fluid" style="margin-top:71px;" id="registerForm">
<div class="row">
<div class="col">
<h3 style="color:#00484F;"><span class="glyphicon glyphicon-play" style="font-size:19px; padding-right:8px;"></span>Register</h3>
</div>
</div>
<br>
<p>Create a new account. Once you've set it up, you can take advantage of<br>the many benefits of membership.</p>


<form action="sign_in.php" method="post" id="register_form">
  <div class="row">
  <div class="form-group col">
    <label for="First_Name" style="font-weight: bold; cursor: text;"><span style="color:red;">* </span>First Name</label>
    <input type="text" name="First_Name" id="First_Name" class="form-control form-control-lg" value="" style="width:505px;
    background-color:#EEEEEE;">
  </div>
</div>
<div class="row">
<div class="form-group col">
  <label for="Last_Name" style="font-weight: bold; cursor: text;"><span style="color:red;">* </span>Last Name</label>
  <input type="text" name="Last_Name" id="Last_Name" class="form-control form-control-lg" value="" style="width:505px;
  background-color:#EEEEEE;">
</div>
</div>

// ... etc

<div class="row">
  <div class="form-group col">
    <input type="submit" class="btn btn-lg btn-info" value="REGISTER" name="submit" style="width:505px;">

In init.php I have the database connection and session_start(); ONLY.

And in the sign_in.php I have the PHP code for the register.php. I have put the PHP code for register in sign_in.php because I want to redirect to sign_in.php when the form in register.php has passed. Now in sign_in.php I have these codes.

<?php
 require_once 'core/init.php';
 include 'includes/head.php';
 include 'includes/navigation.php';
 $msg = "";
 $msg_2 = "";
 $msg_3 = "";
if(isset($_POST['submit'])) {
   $First_Name = sanitize($_POST['First_Name']);
   $Last_Name = sanitize($_POST['Last_Name']);
   $email = sanitize($_POST['email']);
   $confirm_email = sanitize($_POST['confirm_email']);
   $mobile_number = sanitize($_POST['mobile_number']);
   $password = sanitize($_POST['password']);
   $confirm_password = sanitize($_POST['confirm_password']);
   $gender = sanitize($_POST['gender']);
   $day = sanitize($_POST['day']);
   $month = sanitize($_POST['month']);
   $year = sanitize($_POST['year']);
   $insurance = sanitize($_POST['insurance']);
   $sql = $db->query("SELECT * FROM customers WHERE email ='$email'");
   if($sql->num_rows > 0) {
     $msg = "Email already exists in the database!";
   } else {
     $token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM123456789!$/()*';
     $token = str_shuffle($token);
     $token = substr($token, 0, 10);
     $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
     $db->query("INSERT INTO customers (first_name,last_name,email,mobile_number,password,gender,date_of_birth,insurance,
               isEmailConfirmed,token) VALUES ('$First_Name', '$Last_Name', '$email', '$mobile_number', '$hashedPassword', '$gender',
               '$day $month $year','$insurance', '0', '$token');"); 

// Email configuration ...

if($mail->Send())
                 $msg_2 = "<span class='text-success'>For the protection of your information, you must verify your email address to
                 continue registration. Instructions on how to verify your email address have been sent to your email address.
                 Please check your inbox.</span>";
}
}
?>
<div class="container-fluid" style="margin-top:71px;" id="signInForm">
   <div class="row">
 <div class="col-lg-6">

   <?php if($msg_2 != "") echo $msg_2 ?>

<h3 style="color:#00484F;"><span class="glyphicon glyphicon-play"
style="font-size:19px; padding-right:8px;"></span>Sign In</h3>
<br>

<?php if($msg_3 != "") echo $msg_3 ?>

<form action="sign_in.php" method="post" id="sign_in_form">
  <div class="form-group">
    <label for="email" style="font-weight: bold; cursor: text;"><span style="color:red;">* </span>Email Address</label>
    <input type="text" name="email_3" id="email_3" class="form-control form-control-lg" value="" style="width:455px;
    background-color:#EEEEEE;">
  </div>

//etc ...

Now I need this message in sign_in.php $msg = "Email already exists in the database!"; to print in register.php above the form like so

<?php if($msg != "") echo $msg ?>
<form action="register.php" method="post" id="register_form">

if the user entered an email that is already in the database and STAY IN THAT PAGE until the user enter an email that is not in the database. And if the user entered a new email I want to redirect to the sign_in.php page with the message

$msg_2 = "<span class='text-success'>For the protection of your information, you must verify your email address to
                 continue registration. Instructions on how to verify your email address have been sent to your email address.
                 Please check your inbox.</span>";

Above the form like so

<?php if($msg_2 != "") echo $msg_2 ?>

<h3 style="color:#00484F;"><span class="glyphicon glyphicon-play"
style="font-size:19px; padding-right:8px;"></span>Sign In</h3>
<br>

<?php if($msg_3 != "") echo $msg_3 ?>

<form action="sign_in.php" method="post" id="sign_in_form">

What is the suitable code to do that and where should I put it ??

EDIT: I've already have session_start(); in init.php. So, I used sessions so I've replaced

$msg = "Email already exists in the database!";

with

$_SESSION['msg'] = "Email already exists in the database!";

And add in register.php

<?php echo  $_SESSION['msg']; ?>

Above

<form action="register.php" method="post" id="register_form">

So, the sign_in.php will be

<?php
     require_once 'core/init.php';
     include 'includes/head.php';
     include 'includes/navigation.php';
     $msg = "";
     $msg_2 = "";
     $msg_3 = "";
    if(isset($_POST['submit'])) {
       $First_Name = sanitize($_POST['First_Name']);
       $Last_Name = sanitize($_POST['Last_Name']);
       $email = sanitize($_POST['email']);
       $confirm_email = sanitize($_POST['confirm_email']);
       $mobile_number = sanitize($_POST['mobile_number']);
       $password = sanitize($_POST['password']);
       $confirm_password = sanitize($_POST['confirm_password']);
       $gender = sanitize($_POST['gender']);
       $day = sanitize($_POST['day']);
       $month = sanitize($_POST['month']);
       $year = sanitize($_POST['year']);
       $insurance = sanitize($_POST['insurance']);
       $sql = $db->query("SELECT * FROM customers WHERE email ='$email'");
       if($sql->num_rows > 0) {
         $_SESSION['msg'] = "Email already exists in the database!";
       } else {
         $token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM123456789!$/()*';
         $token = str_shuffle($token);
         $token = substr($token, 0, 10);
         $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
         $db->query("INSERT INTO customers (first_name,last_name,email,mobile_number,password,gender,date_of_birth,insurance,
                   isEmailConfirmed,token) VALUES ('$First_Name', '$Last_Name', '$email', '$mobile_number', '$hashedPassword', '$gender',
                   '$day $month $year','$insurance', '0', '$token');"); 

    // Email configuration ...

    if($mail->Send())
                     $msg_2 = "<span class='text-success'>For the protection of your information, you must verify your email address to
                     continue registration. Instructions on how to verify your email address have been sent to your email address.
                     Please check your inbox.</span>";
    }
    }
    ?>
    <div class="container-fluid" style="margin-top:71px;" id="signInForm">
       <div class="row">
     <div class="col-lg-6">

       <?php if($msg_2 != "") echo $msg_2 ?>

    <h3 style="color:#00484F;"><span class="glyphicon glyphicon-play"
    style="font-size:19px; padding-right:8px;"></span>Sign In</h3>
    <br>

    <?php if($msg_3 != "") echo $msg_3 ?>

    <form action="sign_in.php" method="post" id="sign_in_form">
      <div class="form-group">
        <label for="email" style="font-weight: bold; cursor: text;"><span style="color:red;">* </span>Email Address</label>
        <input type="text" name="email_3" id="email_3" class="form-control form-control-lg" value="" style="width:455px;
        background-color:#EEEEEE;">
      </div>

    //etc ...

And register.php will be

<?php
require_once 'core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
?>

<div class="container-fluid" style="margin-top:71px;" id="registerForm">
<div class="row">
<div class="col">
<h3 style="color:#00484F;"><span class="glyphicon glyphicon-play" style="font-size:19px; padding-right:8px;"></span>Register</h3>
</div>
</div>
<br>
<p>Create a new account. Once you've set it up, you can take advantage of<br>the many benefits of membership.</p>

<?php echo  $_SESSION['msg']; ?>
<form action="sign_in.php" method="post" id="register_form">
  <div class="row">
  <div class="form-group col">
    <label for="First_Name" style="font-weight: bold; cursor: text;"><span style="color:red;">* </span>First Name</label>
    <input type="text" name="First_Name" id="First_Name" class="form-control form-control-lg" value="" style="width:505px;
    background-color:#EEEEEE;">
  </div>
</div>
<div class="row">
<div class="form-group col">
  <label for="Last_Name" style="font-weight: bold; cursor: text;"><span style="color:red;">* </span>Last Name</label>
  <input type="text" name="Last_Name" id="Last_Name" class="form-control form-control-lg" value="" style="width:505px;
  background-color:#EEEEEE;">
</div>
</div>

// ... etc

<div class="row">
  <div class="form-group col">
    <input type="submit" class="btn btn-lg btn-info" value="REGISTER" name="submit" style="width:505px;">

But I'm getting an error message in register.php that says

Notice: Undefined index: msg in C:\wamp64\www\tutorial\register.php on line 17

Related Questions