douzhizao0270 2015-06-23 12:14
浏览 20
已采纳

Php使用Ajax登录

I am trying to make a login with ajax, I want the user to login and if the username(email) and password are correct the page should use the method .load to load the page "perfil.php", that I won't post the code since it isn't relevant. However nothing is happening even if i type random user/password.

I just started with SESSION's and AJAX so any help would be appreciated, BTW i don't need answers talking about hashing the password at this moment.

index

<?php
    session_start();
    if(!empty($_SESSION['login_user'])){
        header('Location: user/perfil.php');
    }
?>

<form class="form-signin" id="form-signin" method="post" action="">
<h2 class="form-signin-heading">Sign in</h2>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="true" autofocus="true">
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required="true">
<div class="checkbox">
    <label>
        <input type="checkbox" value="remember-me"> Lembrar-me
    </label>
</div>
<button id="entrar" class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Sign in</button>

login.php

<?php
include_once '../config/db.php';
session_start();
if (isSet($_POST['email']) && isSet($_POST['password'])) {
    try {
        $pdo = Database::connect();
        $email = $_POST['email'];
        $password = $_POST['password'];

        $query= "SELECT id FROM utilizador WHERE email = '$email' and pass = '$password'";

        $stmt = $pdo->prepare($query);
        $stmt->execute();  
        $num=$stmt->rowCount();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if($num > 0)
            $_SESSION['login_user']=$row['id'];
            echo $row['id'];    
        }
        Database::disconnect();
    }
    catch (Exception $e){
        echo 'Exception: ',  $e->getMessage(), "
";
    }
}
?>

javascript

$('#entrar').click(function()
{
    var email = $("#inputEmail").val();
    var password = $("#inputPassword").val();
    var dataString = 'email='+email+'&password='+password;
    if($.trim(username).length>0 && $.trim(password).length>0)
    {
        $.ajax({
            type: "POST",
            url: "user/login.php",
            data: dataString,
            cache: false,
            beforeSend: function(){ $("#entrar").val('A entrar...');},
            success: function(data){
                if(data)
                {
                    showNotas();
                    $('#form-signin').fadeOut('slow', function(){
                        $("#section-1 .container").load("user/perfil.php").fadeIn(1500).delay(5000);
                        $("#section-1 span").text("Perfil");
                    });
                    $('#notas').show();
                }
                else
                {
                    $("#entrar").val('Sign in')
                    $("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
                }
            }
        }); 
    }
return false;
});

EDIT: Chrome dev isn't displaying any error

  • 写回答

4条回答 默认 最新

  • dongwuqi4243 2015-06-23 21:18
    关注

    I'm not sure why but i changed the $('#entrar').click(function( event ) to function entrar() and moved it outside the $(document).ready(function() and it worked.. Thanks everyon anyway, Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题