简体   繁体   English

Jquery AJAX PHP登录

[英]Jquery AJAX PHP login

I have a login form using jquery ajax and a php code. 我有一个使用jquery ajax和php代码的登录表单。 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. 这曾经工作大约一个星期前,直到我不小心删除了jquery,现在我无法让它工作。

Here is the PHP code: 这是PHP代码:

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: 这是Jquery AJAX代码:

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

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 在你的PHP方面,尝试输出更多的东西 - 例如,在你的if语句中添加一个}else{返回一些有用的数据

 }else{
    print("Post is: \n");
    print_r($_POST);
    print("\nMySQL gave me: \n");
    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字段不同
  • db fields are blank for some reason 由于某种原因,db字段为空
  • POST data is wrong / wrong field names POST数据是错误/错误的字段名称

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. 您的代码在表单提交之前获取值 - 在加载时 - 当它们为空时,因此将空字符串作为用户名和密码发送到服务器。

Take a look at the code i have in this answer here: user check availability with jQuery 看看我在这个答案中的代码: 用户使用jQuery检查可用性

You need to json encode your response, also Firefox has Firebug you can use to view your ajax post and response, chrome and IE both have developer tools 你需要json编码你的响应,Firefox也有Firebug你可以用来查看你的ajax帖子和响应,chrome和IE都有开发工具

Safari also has dev tools: http://developer.apple.com/technologies/safari/developer-tools.html Safari还有开发工具: http//developer.apple.com/technologies/safari/developer-tools.html

Check the console to see what is happening with your AJAX 检查控制台,看看你的AJAX发生了什么

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

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