title: How can I redirect users to different pages after inserting into the database using Sweet Alert in PHP?
body: <p>I am finding it difficult to use the sweet alert after a successful login and also add the sweet alert after a user successfully inserts his/her data into the database. I am finding it difficult to use the sweet alert after a successful login and also add the sweet alert after a user successfully inserts his/her data into the database.x</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;?php
          include ("dbcon.php");
  
          session_start();
  
          if(isset($_POST['login'])){
  
              //get the user data
              $username = mysqli_real_escape_string($conn , $_POST['username']);
              $email = mysqli_real_escape_string($conn , $_POST['email']);
              $pwd = mysqli_real_escape_string($conn , $_POST['pdf]);
              
              if(empty($username) || empty($email) || empty($pwd)){
  
                  header("Location:../../login.php?loginempty");
                  exit();
  
              }else{
  
                  $select = "SELECT * FROM users WHERE email ='$email' ";
                  $result = mysqli_query($conn , $select);
                  $result_check = mysqli_num_rows($result);
  
                  if($result_check &lt; 1){
                      header("Location:../../login.php?invaliduid");
                      exit();
  
                  }else{
  
                      if($row=mysqli_fetch_assoc($result)){
  
                          $cpwd = $row['pwd'];
                          
                          //de-hashed the password
                          // $hashedpwd = password_verify($pwd, $row['pdf]);
                          
                          if($pwd != $cpwd){
                              header("Location: ../../login.php?invalidpwd");
                              exit();
          
                          }elseif($pwd == $cpwd){
                              
                              $_SESSION['uid'] =$row['username'];
                              $username = $_SESSION['uid'];
  
                              echo "
                                  Swal.fire({
                                      title: 'Welcome to bottles beach',
                                      text: 'You successfully submitted the form,
                                      icon: 'success',
                                      showCancelButton: false,
                                      confirmButtonColor: '#3085d6',
                                      confirmButtonText: 'Great, show me the site!'
                                  }).then((result) =&gt; {
                                      if (result.confirmed) {
                                      location = '../../dashboard.php'
                                      }
                                  })
                              
                              ";
  
                              // header("Location:../../dashboard.php?loginsuc");
                              // exit();
                          }
          
                      }
                  }
              }
  
          }
      ?&gt;
      
      
      
      &lt;?php
      session_start();
      include ("dbcon.php");
  
  
      if(isset($_POST["reg-btn"])) {
          $username = mysqli_real_escape_string($conn , $_POST['username']);
          $email = mysqli_real_escape_string($conn , $_POST['email']);
          $phone = mysqli_real_escape_string($conn , $_POST['phone']);
          $pwd = mysqli_real_escape_string($conn , $_POST['pwd']);
          $cpwd = mysqli_real_escape_string($conn , $_POST['cpwd']);
          // $verify_token = md5(random());
  
          // VALIDATION
          if(strlen(trim($pwd)) &lt; 5) {
              // Password Length
              header("Location:../../register.php?passwordshort");
              exit();
          }
          
          if($pwd != $cpwd ) {
              // If Password = Confirm Password
              header("Location:../../register.php?comfirmpass");
              exit();
          }
  
          // Check for Empty Inputs
          if (empty($username)|| empty($email)|| empty($phone)|| empty($pwd)|| empty($cpwd) ) {
  
              header ("Location:../../register.php?signupempty");
              exit();
  
          }else {
  
              //VALIDATE EMAIL
              if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
  
                  header ("Location:../../register.php?invalidemail");
                  exit();
  
              }else {
  
                  $select = "SELECT * FROM users WHERE username = '$username' AND pwd ='$pwd'";
                  $result = mysqli_query($conn , $select);
                  $result_check = mysqli_num_rows($result);
  
                  if($result_check &gt; 0) {
  
                      echo "ooooops!!! user already exists";
                  }else {
          
                      $insert = "INSERT INTO users (username, email, phone, pwd, cpwd) VALUES ('$username', '$email', '$phone', '$pwd', '$cpwd')";
                      $result = mysqli_query($conn , $insert);
          
                      if(!$result) {
          
                          echo "ooooops!!! an error was encountered while inserting data to the database";
                      }else {
          
                          echo "
                          Swal.fire({
                              title: 'Welcome to bottles beach',
                              text: 'You successfully submitted the form,
                              icon: 'success',
                              showCancelButton: false,
                              confirmButtonColor: '#3085d6',
                              confirmButtonText: 'Great, show me the site!'
                          }).then((result) =&gt; {
                              if (result.confirmed) {
                              location = '../../login.php'
                              }
                          })
                      
                      ";
                          // header("location: ../../login.php");
                          // exit();
                      }
                  }
              }
              
          }
      
      }else {
  
          header ("Location:../../register.php?signupempty");
          exit();
      }
  
  ?&gt;</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
      $page_title = "Login Form";
      include ('assets/includes/header.php');
      include ('assets/includes/navbar.php');
  ?&gt;
  
  &lt;div class="py-5"&gt;
      &lt;div class="container"&gt;
          &lt;div class="row justify-content-center"&gt;
              &lt;div class="col-md-6"&gt;
                  &lt;div class="card shadow"&gt;
                      &lt;div class="card-header"&gt;
                          &lt;h5&gt;Login Form&lt;/h5&gt;
                      &lt;/div&gt;
  
                      &lt;div class="card-body"&gt;
                          &lt;form action="assets/action/login_action.php" method="POST" autocomplete="no"&gt;
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="username"&gt;Username&lt;/label&gt;
                                  &lt;input type="text" name="username" id="username" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="email"&gt;Email&lt;/label&gt;
                                  &lt;input type="email" name="email" id="email" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="pwd"&gt;Password&lt;/label&gt;
                                  &lt;input type="text" name="pwd" id="pwd" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group"&gt;
                                  &lt;button type="submit" class="btn btn-primary" name="login"&gt;Login Now&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;?php
      include ('assets/includes/footer.php');
  ?&gt;
  
  
  
  
  
  
  
  &lt;?php
  
      $page_title = "Registration Form";
      include ('assets/includes/header.php');
      include ('assets/includes/navbar.php');
  
      // include ('assets/action/dbcon.php');
  ?&gt;
  
  &lt;div class="py-5"&gt;
      &lt;div class="container"&gt;
          &lt;div class="row justify-content-center"&gt;
              &lt;div class="col-md-6"&gt;
                  &lt;div class="card shadow"&gt;
                      &lt;div class="card-header"&gt;
                          &lt;h5&gt;Registration Form With Email Verification&lt;/h5&gt;
                      &lt;/div&gt;
  
                      &lt;div class="card-body"&gt;
                          &lt;form action="assets/action/code.php" method="POST"&gt;
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="username"&gt;Username&lt;/label&gt;
                                  &lt;input type="text" name="username" id="username" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="email"&gt;Email&lt;/label&gt;
                                  &lt;input type="email" name="email" id="email" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="phone"&gt;Phone Number&lt;/label&gt;
                                  &lt;input type="text" name="phone" id="phone" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="pwd"&gt;Password&lt;/label&gt;
                                  &lt;input type="text" name="pwd" id="pwd" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group mb-3"&gt;
                                  &lt;label for="cpwd"&gt;Confirm Password&lt;/label&gt;
                                  &lt;input type="text" name="cpwd" id="cpwd" class="form-control" autocomplete="off"&gt;
                              &lt;/div&gt;
  
                              &lt;div class="form-group"&gt;
                                  &lt;button type="submit" name="reg-btn" class="btn btn-primary"&gt;Register Now&lt;/button&gt;
                              &lt;/div&gt;
                          &lt;/form&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  
  &lt;?php
      include ('assets/includes/footer.php');
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>difficult adding sweet alert redirection after a user successfully inserts his / her details</p>