javascript - AJAX/PHP login - error messages not working -


i have login system user can enter in credentials , redirect them either "client" view or "admin" view of website. working properly.

my issues when user types in incorrect credential information error messages being displayed, i.e. invalid username , password.

here login form:

<form name="ajaxform" id="ajaxform" action="auth/ajax-auth.php" method="post" >     <h4>client login</h4>         <div id="error"></div>             <label for="username" class="col-sm-2 control-label">username</label>             <input id="username" class="form-control" name='username' placeholder="username">             <label for="password" class="col-sm-2 control-label">password</label>             <input type="password" class="form-control" id="password" name='password' placeholder="password">         </div>         <div class="modal-footer">             <button type="button">close</button>             <button type="submit" id="loginbtn">login</button>         </div> </form> 

now if user enters in bad credentials error message should displayed within <div id="error"></div>.

here ajax-auth.php looks like:

session_start();  error_reporting(e_all);                //this amazing0 ini_set('display_errors', 1);          // <----  $username = $_post['username']; $password = $_post['password'];  include '../connection.php'; $dbc = mysqli_connect(db_host, db_user, db_password, db_name);  $sql = "select * users username='$username' , password='$password'";  $result = mysqli_query($dbc, $sql);      if (mysqli_num_rows($result) == true)     {         $_session["username"] = $username;          $row = mysqli_fetch_array($result);         if ($row["permission"] == 2)         {             $_session["permission"] = 2;             // redirect admin login page.             echo 'admin';         }         else if ($row["permission"] == 1)         {             $_session["permission"] = 1;             $_session["userid"] = $row["user_id"];             $_session["account"] = $row["account_id"];             echo 'client';         }     }     else     {         echo "user not found!";         header("location: ../index.html");     } 

and here ajax function handles redirecting.

$("#ajaxform").submit(function(e)                           {         e.preventdefault();         var username=$("#username").val();         var password=$("#password").val();         var datastring = 'username='+username+'&password='+password;         if($.trim(username).length>0 && $.trim(password).length>0) {             $.ajax({                 type: "post",                 url: "auth/ajax-auth.php",                 data: datastring,                 cache: false,                 beforesend: function(){ $("#loginbtn").val('connecting...');},                 success: function(data){                     if(data) {                         //alert(data + " logged in!");                         if(data == "admin"){                            window.location.href = "/ttw-website-final/admin/admin.php";                        }                        else if(data == "client"){                          //uncomment when index.php page ready                         //window.location.href = "index.php";                          window.location.href = "index.html";                         }                      } else {                         $("#error").html("<span style='color:#cc0000'>error:</span> invalid username , password. ");                         $("#loginbtn").val('submit')                     }                 }              });                     } 

when enter in bad credentials form , hit login nothing happens @ , can't seem figure out why. should displaying error message in ajax function.

$("#error").html("<span style='color:#cc0000'>error:</span> invalid username , password. "); 

what doing wrong here?

i know close getting work how need , it's silly overlooking, appreciated.

with of jeroen understand doing wrong.

i should have echoed out value if results query returned false , have third option in ajax function deals value returned success function.

here changed in regards php:

if(mysqli_num_rows($result) == true)     {         $_session["username"] = $username;          $row = mysqli_fetch_array($result);         if ($row["permission"] == 2)         {           $_session["permission"] = 2;           // redirect admin login page.           echo 'admin';         }         else if ($row["permission"] == 1)         {            $_session["permission"] = 1;            $_session["userid"] = $row["user_id"];            $_session["account"] = $row["account_id"];            echo 'client';         }     }     else     {         echo "unauthorized";     } 

and within ajax function added third option handle happens returned value.

if(data) {    if(data == "admin"){         window.location.href = "/ttw-website-final/admin/admin.php";     }     else if(data == "client"){         window.location.href = "index.html";     }     else if(data == "unauthorized"){        $("#error").html("<span style='color:#cc0000'>error:</span> invalid username , password. ");        $("#loginbtn").val('submit')     } } 

doing resolved issue , error messages being displayed if add in incorrect credentials.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -