doucheng4094 2012-01-27 01:19
浏览 44
已采纳

Jquery AJAX PHP登录

I have a login form using jquery ajax and a php code. The issue is that is always returns an error instead of logging me into the system. I have spent days trying to find the error but I can't. The webpage displays no errors if I visit it directly. This used to work about a week ago until I accidentally deleted the jquery and now I can't get it to work.

Here is the PHP code:

include('.conf.php');
$user = $_POST['user'];
$pass = $_POST['pass'];
$result = mysql_query("SELECT * FROM accountController WHERE username = '$user'");
while ($row = mysql_fetch_array($result)) {
    if (sha1($user.$pass) == $row['pword']) {
        setcookie('temp', $row['username']);
        session_start();
        $_SESSION['login'] = 1;
        $_SESSION['uname'] = $row['username'];
        echo "success";
    }
} 

and here is the Jquery AJAX code:

var username = $('#main_username').val();
var password = $('#main_pword').val();
    $('.mainlogin').submit(function() {
        $.ajax({
            url: 'log.php',
            type: 'POST',
            data: {
              user: username,
              pass: password
            },
            success: function(response) {
                if(response == 'success') {
                    window.location.reload();
                } else {
                    $('.logerror').fadeIn(250);
                }
            }
        });
        return false;
    });

How would I check to see what is being returned like blank or success from the server. Is there any extension for safari? Thanks!

  • 写回答

2条回答

  • douce1368 2012-01-27 01:22
    关注

    Put this in, instead. It will alert the server's response in a popup.

     success: function(response) {
         alert(response);
     }
    

    On your PHP side, try outputting more stuff - add an }else{ to your if statement that returns some useful data, for example

     }else{
        print("Post is: 
    ");
        print_r($_POST);
        print("
    MySQL gave me: 
    ");
        print_r($row); 
     }
    

    Just make sure you don't leave it in once it's working!

    Look for:

    • hash is different
    • db field is different
    • db fields are blank for some reason
    • POST data is wrong / wrong field names

    Edit: Here's another issue: Your first four lines should actually be:

        $('.mainlogin').submit(function() {
            var username = $('#main_username').val();
            var password = $('#main_pword').val();
            $.ajax({
    

    Your code as it is gets the values before the form is submitted - at load - when they are blank, and as a result is sending blank strings to the server as username and password.

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况