2015-01-04 07:31
采纳率: 100%
浏览 40


I have 3 files: 1. Simple form with email and password 2. login_validate.php to validate form input against database 3. login.js to perform ajax task

I tried to validate form with just login_validate.php and everything worked for me. But when i tried to use ajax(login.js), it always tells me wrong email and password even I tried to type in correct email and password in form. Below is my code, please advice what is wrong with it?

Here is my form:

<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>

Here is my login_validate.php:

$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;
echo 0;

Here is my login.js:

$('#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
        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/';
                alert('Problem with sql query');
    return false;


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drxm72811 2015-01-04 08:01

    No need to test the dangnhap.

    in PHP remove the check:

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

    Change in JS:

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

    in HTML:

    remove the type( type="submit" ) attribute as you are using <button>

    解决 无用
    打赏 举报
  • dongou3286 2015-01-04 07:47

    in login_validate.php


    but in login.js dangnhap is not set.

    in $.ajax({ data can be an object, you dont have to make it a string

    解决 无用
    打赏 举报

相关推荐 更多相似问题