在同一页面上回显成功或失败

时间:2017-05-09 16:52:06

标签: php ajax

这是我的注册表单。

  <div class="tab-content">
    <div id="signup">   
      <h1>Sign Up for Free</h1>

  <form action="register.php" method="POST">

  <div class="top-row">
    <div class="field-wrap">
      <label>
        Username<span class="req">*</span>
      </label>
      <input name="username" type="text" required autocomplete="off" />
    </div>

    <div class="field-wrap">
      <label>
        E-mail<span class="req">*</span>
      </label>
      <input name="email" type="text"required autocomplete="off"/>
    </div>
  </div>

  <div class="field-wrap">
    <label>
      Password<span class="req">*</span>
    </label>
    <input name="password" type="password"required autocomplete="off"/>
  </div>

  <div class="field-wrap">
    <label>
      Repeat password<span class="req">*</span>
    </label>
    <input name="rpassword" type="password"required autocomplete="off"/>
  </div>
  <button type="submit" class="button button-block"/>Get Started</button>
  </form>

</div>

<div id="login">   
  <h1>Welcome Back!</h1>

  <form action="login.php" method="post">

    <div class="field-wrap">
    <label>
      Username<span class="req">*</span>
    </label>
    <input name="username" type="text"required autocomplete="off"/>
  </div>

  <div class="field-wrap">
    <label>
      Password<span class="req">*</span>
    </label>
    <input name="password" type="password"required autocomplete="off"/>
  </div>

  <p class="forgot"><a  href="forgot.php">Forgot Password?</a></p>

  <button class="button button-block"/>Log In</button>

  </form>

</div>

然后是register.php

<?php
session_start();


$host= 'localhost';
$user='root';
$pass='';
$db='gameforum';


$conn=mysqli_connect($host, $user, $pass, $db);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
}

    $username = $_POST['username'];
    $password = $_POST['password'];
    $rpassword = $_POST['rpassword'];
    $email = $_POST['email'];

if ($password!==$rpassword) {
    echo "Passwords did not match, please try again!"
    $conn->close();
}
else {

$sql = "INSERT INTO users (username, password, Repeat_Password, email) 
VALUES ('$username', '$password', '$rpassword', '$email')";

  if ($conn->query($sql) === TRUE) {
       $redirectUrl = 'index.php';

    echo '<script type="application/javascript">alert("Thank you for your registration! You may now log in with your account!"); window.location.href = "'.$redirectUrl.'";</script>';
}else{
    echo "Username or Email already exists!". mysql_error();
  }

$conn->close();
}


?>

问题是如果密码不匹配,那么用户会进入一个空白页面。我希望错误消息在用户注册的同一页面上弹出。我不知道怎么做,有什么想法吗?

2 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点。一种是使用会话,另一种是将用户重定向到登录页面,例如signin.php?err = invalid_pass

您使用会话执行此操作,您需要在登录页面顶部使用此代码。如果您在登录页面中设置了任何内容,它将打印出错误消息。

<?php
session_start();
if(isset($_SESSION['err']))
{
echo $_SESSION['err'];
unset($_SESSION['err'];
}

或者,您可以在登录页面上添加此代码:

<?php
if(isset($_REQUEST['err']))
echo $_REQUEST['err'];

您还需要替换echo "Passwords did not match, please try again!"

如果你做会话,

用这个:

$_SESSION['err']="Passwords did not match, please try again!";
header("Location: signin.php");

如果你使用其他方法,这就是这个。

header("Location: signin.php?err=You_Goofed");

答案 1 :(得分:0)

您需要在HTML页面上使用ajax。当用户单击表单按钮时,使用ajax将数据发送到服务器,而不是将表单提交到服务器。然后将数据从服务器返回给客户端。

示例:

$('#submit-btn').click(function(e){
  $.ajax({
    type:'POST',
    url:'register.php',
    data:{
      user:username,
      pw:password
    }
    }.done(function(data){
      if(data === false){
        alert("passwords don't match!");
      }
     });
});

在PHP页面中,您将验证密码。如果他们不匹配,则回复错误。