在PHP中将消息从一页打印到另一页

时间:2019-02-07 14:36:26

标签: php

我正在使用PHP开发网站。我现在有两个文件(两个页面)sign_in.php和register.php。在register.php中,只有注册表单的html代码。

<?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;">

在init.php中,我具有数据库连接和session_start();。

在sign_in.php中,我有register.php的PHP代码。我已将用于注册的PHP代码放在sign_in.php中,因为当register.php中的表单通过时,我想重定向到sign_in.php。现在在sign_in.php中,我有了这些代码。

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

现在我需要sign_in.php中的此消息$ msg =“数据库中已经存在电子邮件!像这样在表单上方的register.php中打印

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

如果用户输入了数据库中已经存在的电子邮件,并且停留在该页面上,直到用户输入了数据库中没有的电子邮件。而且,如果用户输入了一封新电子邮件,我想将其中的消息重定向到sign_in.php页面

$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>";

像这样的表格上

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

执行此操作的合适代码是什么?应该放在哪里?

编辑:我已经有session_start();在init.php中。因此,我使用了会话,所以我替换了

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

使用

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

并添加register.php

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

以上

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

因此,sign_in.php将是

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

然后register.php将是

<?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;">

但是我在register.php中收到一条错误消息,内容为

注意:未定义的索引:第17行的C:\ wamp64 \ www \ tutorial \ register.php中的味精

0 个答案:

没有答案