繁体   English   中英

使用ajax登录表单不起作用

[英]Login form with ajax does not work

我有3个文件:1.具有电子邮件和密码的简单表单2. login_validate.php以针对数据库验证表单输入3. login.js执行ajax任务

我试图仅使用login_validate.php来验证表单,并且一切对我都有效。 但是,当我尝试使用ajax(login.js)时,即使我尝试以表格形式输入正确的电子邮件和密码,它也总是告诉我错误的电子邮件和密码。 以下是我的代码,请告诉我这是什么问题?

这是我的表格:

<div id="login_result"></div>
<form action="login_validate.php" method="post">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
<input type="password" class="form-control" id="pass" name="pass" placeholder="Password">
<button type="submit" class="btn btn-default" name="submit" id="submit">Submit</button>
</form>

这是我的login_validate.php:

<?php
require("configs/dbconnect.php");
if(isset($_POST["dangnhap"])){
$email=mysql_real_escape_string($_POST["email"]);
$pass=mysql_real_escape_string($_POST["pass"]);
$sql = 'SELECT name, email, pass, visible FROM user WHERE email = "'.$email.'" AND pass =     "'.$pass.'"';
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
$num_rows= mysql_num_rows($result); // Get the number of rows
if($num_rows > 0){
$row = mysql_fetch_array($result);
echo 1;
$_SESSION["email"]=$row["email"];
$_SESSION["pass"]=$row["pass"];
$_SESSION["name"]=$row["name"];
}
else{
echo 0;
}
}
?>

这是我的login.js:

$(document).ready(function(){
$('#email').focus(); // Focus to the username field on body loads
$('#submit').click(function(){ // Create `click` event function for login
    var email = $('#email'); // Get the username field
    var pass = $('#pass'); // Get the password field
    var login_result = $('#login_result'); // Get the login result div
    login_result.html('loading..'); // Set the pre-loader can be an animation
    if(email.val() == ''){ // Check the username values is empty or not
        email.focus(); // focus to the filed
        login_result.html('<span class="error">Enter the username</span>');
        return false;
    }
    if(pass.val() == ''){ // Check the password values is empty or not
        pass.focus();
        login_result.html('<span class="error">Enter the password</span>');
        return false;
    }
    if(email.val() != '' && pass.val() != ''){ // Check the username and password values is not empty and make the ajax request
        var UrlToPass = 'email='+email.val()+'&pass='+pass.val();
        $.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
        type : 'POST',
        cache: false,
        data : UrlToPass,
        url  : 'login_validate.php',
        success: function(responseText){ // Get the result and asign to each cases
            if(responseText == 0){
                login_result.html('<span class="error">Username or Password Incorrect!</span>');
            }
            else if(responseText == 1){
                window.location = 'member/';
            }
            else{
                alert('Problem with sql query');
            }
        }
        });
    }
    return false;
});

});

在login_validate.php中

if(isset($_POST["dangnhap"])){

但是在login.js中,未设置dangnhap。

在$ .ajax({数据可以是一个对象,您不必使其成为字符串

无需测试dangnhap。

在PHP中删除检查:

isset($_POST["dangnhap"])  //remove it, no checking for submit required. done in JS file.

更改JS:

var UrlToPass = {email:email.val(),pass:pass.val()} ;

在HTML中:

在使用<button>删除type( type="submit" )属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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