0

I have a pop up registration form, registration works well but if user inputs invalid email and submit the form it closes and when user opens it another time or refreshes the page then only the error appears means error message works but not at proper time or I don't know what is happens.

This is my PHP registration code :

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

    ?>

This is my html pop up form code :

<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>
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159

1 Answers1

0

Actually when you submit your form. it will reload the page. and definitely popup will close after submit the form.

So what I will suggest you to submit form using ajax and display success/error message according to ajax response.

Refer this link to know how you can submit form using ajax

Community
  • 1
  • 1
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13