我无法通过包含语句传递变量

  • 本文关键字:语句 变量 包含 php html
  • 更新时间 :
  • 英文 :


我从PHP开始我做一个注册表单将信息传递给register。PHP文件然后该文件中的脚本检查密码字段值是否等于密码确认字段值如果不相等,则为$message变量设置一个值然后重定向到index。PHP文件其中$message变量应该在p标签中显示但不打印然后

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

,这是register.php文件

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

因为在register.php中你首先检查

isset($_POST['submit'])

但是没有post请求,只有你可以访问$_GET['error-in-password']

对于回显页面中的消息,你需要输入

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

在register.php中你需要

if (isset($_GET['error-in-password']))
{
echo $_GET['message'];
}

最新更新