2017-05-25 16:51
浏览 51

如何使jQuery / Ajax日志返回“错误的凭据”错误?

I'm making a dynamic PHP page with MySQL that has users log in. I recently changed the login, to jQuery/AJAX to make it more smooth and not lose the current page users are in. The thing is that when I miss my log in credentials it doesn't how any message and I want it to do so.

Here is my form :

<form id="ajax-login-form" action="session/login.php" method="post" role="form" autocomplete="off">
                <div class="form-group">
                    <label for="username">Login</label>
                    <input type="text" name="u" id="username" tabindex="1" class="form-control" placeholder="Log in" value="" autocomplete="off">

                <div class="form-group">
                    <label for="password">Password</label>
                    <input type="password" name="p" id="password" tabindex="2" class="form-control" placeholder="Password" autocomplete="off">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-5 pull-right">
                            <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-success" value="Log In">

My jQuery/Ajax :

$(document).ready(function () {
//From insert
$("#login-submit").click(function () {
    var $form = $('#ajax-login-form');
    $.post($form.attr("action"), $form.serializeArray(), function (info) {

And finally my php code

$lig = mysql_connect("localhost", "root","") or
die ("Problema na ligação ao servidor MYSQL");
mysql_select_db("demo", $lig);


$sql="select numuti,nome,nomeutilizador,codtipo,reset from utilizadores where nomeutilizador='$u' and password=md5('$p')";
if (mysql_num_rows($res) == 1)
    $lin = mysql_fetch_array($res, MYSQL_ASSOC);
        $_SESSION['user'] = $lin['nomeutilizador'];
        $_SESSION['nivel'] = $lin['codtipo'];
        $_SESSION['reset'] = $lin['reset'];
        //$_SESSION['foto']  = $lin['imagem'];
        $_SESSION['nome']  = $lin['nome'];
        $_SESSION['cod']= $lin['numuti'];
echo "<div class='alert alert-danger'>
            <strong><center>Login Inválido, Tente Novamente</center></strong>

This is probably a duplicate question, but I couldn't find a reliable answer to my issue, I tried making the errors with divs as you can see in my login.php page.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duansha8764 2017-05-25 18:04


    If you are using old mysql_connect function vulnerable to SQL injections you do not need any other security .. your system will be permeable.

    First you need to do is switch to better methods like PDO.


    I recommend to use submit() function instead of click() and ajax() instead of post() to better manipulation with data.

    $(document).ready(function () {
      $('#ajax-login-form').submit(function(e) {
          var form = this;
          var username = $("#username").val();
          var password = $("#password").val();
              type: 'POST',
              url: 'Your-login-url',
              data: {
                  username: username,
                  password: password
              success: function(data) {
                  // do your PHP login and echo 1 if authentication was successfull
                  if(data === 1) {
                  } else {
                  // show alert or something that user has wrong credentials ...


    Another thing is error notification. I like to add error div right into the form or form container but set it as hidden. You can show it after jQuery Ajax ends with 0.

    <div id="error_notif">You used wrong credentials.</div>
    #error_notif {
    点赞 打赏 评论
  • dongqi7631 2017-05-25 17:14

    try changing your post statement to this

    $.post($form.attr("action"), $form.serializeArray(), function (info) {
        alert("Login unsuccessful, please try again.");
    点赞 打赏 评论

相关推荐 更多相似问题