0

I am creating a login script that checks if the user exists in the table.

In the event that the user doesn't exist, I want to use jquery to open a modal window letting the user know there was an error.

 <?php
   include("../include/database.php");
   if(isset($_GET['loginSubmit']))   // form submit button
   {
 // form username and password values
     $username = strip_tags(mysqli_real_escape_string($dbc, trim($_GET['username'])));
     $password = strip_tags(mysqli_real_escape_string($dbc, trim(md5($_GET['password']))));

 // set query
     $select = "SELECT username, fullname, userlevel, `password` FROM users WHERE username = '".$username."'"; 
 // run query
     $query = mysqli_query($dbc, $select);
 // get the results
     $row = mysqli_fetch_array($query);
 // set the results to variables to be used in sessions
     $dbusername = htmlentities(stripslashes($row['username']));    
     $dbfullname = htmlentities(stripslashes($row['fullname']));
     $dbuserlevel = htmlentities(stripslashes($row['userlevel']));
     $dbpassword = htmlentities(stripslashes($row['password']));

 // check if the database password matches the user password
     if($dbpassword == $password)
     {
 // if yes, set the sessions and direct to main page
       $_SESSION['username'] = $username;
       $_SESSION['fullname'] = $dbfullname;
       $_SESSION['userlevel'] = $dbuserlevel;
       header("Location:main.html"); 
     }
     else
     {
 // if no match, this is where I want the javascript to show the modal
       echo ("<script language='javascript'>
              $('#errLoginModal').modal('show');  // this is jquery and I know it's incorrect
              window.location.href='index.php'
              </script>");
     }  
 ?>

I am still trying to get better with JavaScript, jQuery, and PHP. With Stack Overflow, I have been getter getting better.

I want the PHP script to use JavaScript/jQuery to fire a modal window when the username/password check fails. I am not even sure if this is the best method.

I know the code above is incorrect. I mean it doesn't show the modal window for one.

On the other hand, I can add an alert, and it fires the alert like so:

 else
 {
   echo ("<script language='javascript'>
          alert(test);
          window.location.href='index.php'
          </script>");
 }

This works. I just would like it to be able to show the modal.

2
  • jQuery UI's dialog widget would serve you best. Commented Apr 6, 2015 at 14:51
  • Are you submitting the form though ajax? If not, you probably should as a modal window does not make a lot of sense on a newly loaded page / in the content of your question. Commented Apr 6, 2015 at 14:56

2 Answers 2

2

You can try to include js file.

 }else{

   echo `<script src="you_file.js"></script>`
 }

    //you_file.js

(function ($){
    $(document).ready(function() {
        alert(`test`);
        $('#errLoginModal').modal('show');  
        window.location.href='index.php';
    })
})

of course include jquery before that

Sign up to request clarification or add additional context in comments.

2 Comments

The jquery file I am using is jQuery-2.1.3.min.js, but it is located near the bottom of my front page, called index.php. Could this be the reason why the script is still not working?
thank you sir. I was able to answer the question using your post comment.
1

I am assuming you're using Bootstrap to open the modal window, because you use .modal('show'). Make sure you include jquery as well as the bootstrap js files in your HTML <head></head>.

In case the modal window is located below the call to $('#errLoginModal').modal('show');, you need to use the jQuery .ready event, which will fire once the DOM is fully loaded, docs.

So you're else should probably look like so:

else {
    echo ("<script language='javascript'>
        $( document ).ready(function() {
          $('#errLoginModal').modal('show');
        });
        </script>");
}

1 Comment

I was able to use a combination of your code and Vlad Mazur's code to figure out my problem. Thank you, sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.