Stop the page to refresh after the form button is clicked and error is shown

Viewed 19

I have seen other questions too but i want to stop my page from refreshing after signup form is filled and with validation from php it shows an error in a span. or not refreshing the form data or not clearing the form data will also work. I just want the form data to be exact as it is but the error to be shown. please find the code

  <form action="signup.php" id="form" method="POST" onsubmit="">
  <a style="text-decoration:none ;" href="index.php"><h1 >Mero <span>Notes</span></h1></a>
  <h3>Register Your Account</h3>  
  <?php 
      echo '<p style="margin-top:10px;color:red;">'.$message.'</p>';
       ?>        
        <p id="validationSpan"></p>
      
        <input placeholder="Full Name" type="text" required="required" name="fullName" value=""/>
        <input placeholder="Email" type="text" required="required" name="email" />
        <input placeholder="Password" type="password" name="password" required="required" id="id_password" minlength="8" onkeyup="passValidation();"/>
        <input placeholder="Confirm Password" type="password" name="conPassword" required="required" id="id_conPassword" onkeyup="passValidation();"/>
        <input placeholder="Contact" type="number" required="required" name="contactNum" />
        <button type="submit" class="regButton" type="submit" value="Sign Up" id="regBtn" onclick="return passValidationAlert()">SignUp </button>
    <h4 style="text-align: right; color:black;font-size:12px;">
      Already Have an Account ?
      <a class="buttomLogin" href="index.php">Login here</a>
    </h4>
  </form>

The php code looks like this

if(mysqli_num_rows($result)>0){
  $_SESSION['error']=true;
    $message='The Entered Email is Already Taken'; 
      }
        elseif($password!=$confirmPassword){
           $message='Password did not match';
      }
      {
           $epassword=password_hash($password,PASSWORD_BCRYPT);
            $sql = "INSERT INTO signupdatabasemn (fullName, email, password, phoneNumber) 
              VALUES ( '$fullName', '$email', '$epassword', $phoneNumber) ";
                $result2= mysqli_query($conn, $sql);


 if ($result2>0) {
    header('Location: /demosite3fnl/index.php');
  } else {
  }
 
}
?>
1 Answers

The simplest solution would be to put the post data in the input value so that when page get refreshed, it stays where it should be. In example:

<input placeholder="Email" type="text" required="required" name="email" value="<?= $_POST['email'] ?>"/>

Updated. Just seen your updated code. Looks like form is sending request to different php file. In this case, try return posted data using get. See more here

Related