简体   繁体   中英

why jQuery/AJAX button don't working

I have a login form that is submitted with a jQuery AJAX request. I would like to use "button" be able to submit the form.but only "button" has no response to function (data)

HTML:

<form action="" method="post" class="form-signin">
    <input type="text" placeholder="Username" id="user" class="form-control loginid">
    <input type="text" placeholder="Password" id="password" class="form-control loginpw">
    <input type="text" placeholder="Check" id="check" class="form-control ck_secure">
    <div class="captcha_block">
      <span id="captcha" class="captcha"></span>
      <a href="#" class="change" onclick="captchaCode()"> <img src="img/change.svg" alt="" class="photo04"></a>
    </div>
    <!-- <a href="#" class="login" id="login">Sign In</a> -->
    <!-- <input id="login" type="button" class="btn btn-lg btn-primary btn-block" value="Sign In"> -->
    <button id="login" class="btn btn-lg btn-primary btn-block" name="login">Sign In</button>

javascript:

$("#login").click(function(){
        if($('#code').text() != $('#check').val()){
          alert('error');
          location.reload();
        }
        else{
          $.post('./process.php',
            {
              type : 'login',
              user : $('#user').val(),
              Passwd: $('#password').val()
            },
            function(data){
              alert(data);
              window.location.href = '../';
          });
        }
      });

process.php:

if($_POST['type'] == 'login'){
  $login_row = @mysql_fetch_assoc(@mysql_query("SELECT * FROM `person` WHERE `user` = '".$_POST['user']."' AND `password` = '".$_POST['Passwd']."'"));
  if($login_row)
  {
   $_SESSION['user'] = $login_row['user_name'];
   echo 'welcome';
  }else
  {
   echo 'Incorrect username/password. please login again';
  }
 }

在“ 登录” type=button中添加属性type=button ,如下所示。

<button type="button" id="login" class="btn btn-lg btn-primary btn-block" name="login">Sign In</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM