错误"电子邮件已经存在"只有在第二次打开后才会出现在弹出窗体中

时间:2017-04-20 05:52:33

标签: php html forms registration

我有一个弹出式注册表单,注册效果很好,但是如果用户输入无效的电子邮件并提交表单,那么当用户再次打开它或刷新页面时,只会出现错误,表示错误消息有效,但不正常时间或者我不知道发生了什么。

这是我的PHP注册码:

<?php 

if (isset($_POST['submit'])) {
    $password = md5($_POST["password"]);
    $link = mysqli_connect("localhost","root","mysql");
    mysqli_select_db($link,"myuser");
    $count = 0;
    $res = mysqli_query($link,"select * from register where email='$_POST[email]'") ;
    $count = mysqli_num_rows($res);

if ($count>0) {
        $emailErr = "Email already exists"; 

    }



  else{
    mysqli_query($link,"insert into register values('','$_POST[username]' ,'$_POST[email]' ,'$password')");
    header("location:index.php");
  }
}

    ?>

这是我的html弹出窗体代码:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<div class="modal2">

<div class="modal2_close close2"></div>

<div class="modal2_main">

        <div class="login-screen">
            <div class="app-title">
                <h1>Sign Up</h1>
            </div>


             <div><img src="images/common.png" class="circle"></div>

            <div class="login-form">
                <div class="control-group">
                <input type="text" class="login-field" value="" placeholder="username" name="username" required pattern="^[A-Za-z0-9]+">
                <label class="login-field-icon fui-user" for="login-name"></label>
                </div>

                <div class="control-group2">
                <input type="text" class="login-field" value="" placeholder="Email" name="email" required pattern="^[A-Za-z0-9]+">
                <label class="login-field-icon fui-user" for="login-name"></label>
                <span><?php echo $emailErr;?></span>
                </div>

                <div class="control-group">
                <input type="password" class="login-field" value="" placeholder="password" name="password" required 
                 pattern="^[A-Za-z0-9]+">
                <label class="login-field-icon fui-lock" for="login-pass"></label>

                </div>

                <button name="submit" class="btn btn-primary btn-large btn-block">Register</button>

            </div>
        </div>

</div>
</div>
</form>

1 个答案:

答案 0 :(得分:0)

实际上,当您提交表单时。它会重新加载页面。提交表格后,弹出窗口肯定会关闭。

那么我建议您使用ajax提交表单并根据ajax响应显示成功/错误消息。

请参阅this链接,了解如何使用ajax

提交表单