title: How to keep my user in the same page when a certain value is not correct
body: <p>So, I am trying to create a little script that should check whether the data filled out by the user is correct or not, my problem however: even if it isn't right, it will throw the alert and then STILL redirect the user to the page where the check will be done (I think maybe because the action of my form field is to send you over there?). My question: How do I keep the user in the same page when the alert pops up that something has not been filled out correctly (without losing the values of the input fields either)?</p>
  
  <p>The code I have:</p>
  
  <pre><code>&lt;?php
      if(!isset($_POST['submit'])) {
          header('Location: errorHandler.php');
          exit();
      } else {
              $email =$_POST['email'];
              $usrName =$_POST['userName'];
              $pwd =$_POST['password'];
              $pwdConfirm =$_POST['confirmPassword'];
  
          if ($email !== $email) {
              echo '&lt;script language="javascript"&gt;';
                  echo 'alert("It seems your email was not correct.")';
              echo '&lt;/script&gt;';
          } else if ($usrName !== $usrName) {
              echo '&lt;script language="javascript"&gt;';
                  echo 'alert("It seems your username was not correct.")';
              echo '&lt;/script&gt;';
          } else if ($pwd !== $pwdConfirm) {
              echo '&lt;script language="javascript"&gt;';
                  echo 'alert("it seems the passwords do NOT match")';
              echo '&lt;/script&gt;';
          } else {
              include_once 'db.inc.php';
  
              $email = mysqli_real_escape_string ($conn, $_POST['email']);
              $usrName =  mysqli_real_escape_string ($conn, $_POST['userName']);
              $pwd =  mysqli_real_escape_string ($conn, $_POST['password']);
              $pwdConfirm =  mysqli_real_escape_string ($conn, $_POST['confirmPassword']);
  
              $sqli = "INSERT INTO logindb (email, name, password) VALUES ('$email', '$usrName', '$pwd')";
  
              mysqli_query($conn, $sqli);
          }
      }
  </code></pre>
  
  <p>How the input fields are created:</p>
  
  <pre><code>function getEblock() {
      let eBlock = ($('&lt;div/&gt;', {
          'class': 'col-md-6 col-md-offset-3 well'
      }));
      return eBlock;
  }
  
  function getEmail() {
      let email = ($('&lt;input/&gt;', {
          'type': 'email',
          'name': 'email',
          'placeholder': 'Fill out your e-mail...',
          'required': true,
          'class': 'form form-control'
      }));
      return email;
  }
  
  function getUserName() {
      let userName = ($('&lt;input/&gt;', {
          'type': 'text',
          'name': 'userName',
          'placeholder': 'Fill out your username...',
          'required': true,
          'class': 'form form-control'
      }));
      return userName;
  }
  
  function getPassword() {
      let password = ($('&lt;input/&gt;', {
          'type': 'password',
          'name': 'password',
          'placeholder': 'Fill out your password...',
          'required': true,
          'class': 'form form-control'
      }));
      return password;
  }
  
  function getConfirmPassword() {
      let confirmPassword = ($('&lt;input/&gt;', {
          'type': 'password',
          'name': 'confirmPassword',
          'placeholder': 'Confirm your password...',
          'required': true,
          'class': 'form form-control'
      }));
      return confirmPassword;
  }
  
  function getConfirmBtn() {
      let submitBtn = ($('&lt;button/&gt;', {
          'type': 'submit',
          'name': 'submit',
          'class': 'btn btn-info',
          'id': 'submitBtn'
      })).text('Submit');
  
      return submitBtn;
  }
  </code></pre>
  
  <p>Yes, I know the way I am inserting everything into the table of my db sucks and I know I have to encrypt the pwd etc. but for now I am wondering how I can prevent the user from (still) going to <code>save.php</code>. My HTML won't show you much since the input fields etc. are made by javascript.</p>