How do I use sweetalert2 inside a php condition in a different file?

Viewed 15

I have a sample log in/log out module. It's inside a form that accesses a separate php file for verification and database access. I want to use sweetalert2 to display the success or denied modal. How can I do that?

HTML:

<div class="modal-body">
    <div class="text-danger"><?php echo isset($_GET['error']) ? $_GET['error']: "" ?></div>
     <form action="login.php" method="POST" enctype="multipart/form-data">
      <div class="form-group">
       <label for="email">Email address</label>
         <input type="email" class="form-control" name="email" placeholder="Enter email" required>
      </div>
      <div class="form-group">
        <label for="password">Password</label>
        <input type="password" class="form-control" name="password" placeholder="Password" required>
      </div>
      <div class="modal-footer">
       <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       <button type="submit" class="submit_btn btn btn-primary">Submit</button>
      </div>
     </form>
    </div> 

As you can see, it's accessing a separate php file. Don't worry about ^this modal, it can be closed. How do I replace this modal with another modal made of sweetalert2 when the php is in another file?

PHP: (login.php)

if (empty($email)) {
        header("Location: index.php?error = You need to enter your email.");
    } else if (empty($password)) {
        header("Location: index.php?error = You need to enter your password.");

A sample of my php code. I know it's bad form to just...redirect like that, but the main problem here is how to pass that error into a sweetalert2. I've tried calling swwetalert2 inside the login.php file, but it doesn't work.

0 Answers
Related