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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)