2

我的代码运行良好,但在成功调用时失败。在 ajax 部分“if (result.success){”上,它在浏览器源选项卡中显示错误,结果为空。

登录页面

<form id="login_form_header">
    <input type="hidden" name="action" value="sc_ajax_callback" />
    <input type="hidden" name="func" value="sc_login" />

    Email:
    <input type="text" name="username" />

    Password:
    <input type="password" name="password"/>

    <input type="submit" value="Log in" />
</form>
<script>
$("#login_form_header").submit(function(event){

    event.preventDefault();

    $.ajax({
        type: 'post',
        url: '<?php echo get_stylesheet_directory_uri(); ?>/connect.php',
        data: $('#login_form_header').serialize(),
        dataType: 'json',
        success: function(result){
            if (result.success){
                window.location = "my-dashboard/";
                return false;
            };
        },
        error: function(e){console.log("Could not retrieve login information")}
    });

    return false;
});
</script>

连接页面

<?php 

    mysql_connect("localhost", "%user%", "%pass%") or die(mysql_error()); // Connect to database server(localhost) with username and password.
    mysql_select_db("%db%") or die(mysql_error()); // Select registration database.
    # Make sure form data was passed to the script
    if(isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['password']) && !empty($_POST['password'])){

    # Define Variables
    $active_var = "1";
    $given_username = mysql_escape_string($_POST['username']);
    $given_password = dosomethingtopass;
    $matched_username = "";
    $matched_password = "";

     # See if there is matching info in the database
    $sql = 'SELECT username, password, active FROM %table% WHERE username="'.$given_username.'" AND password="'.$given_password.'" AND active="'.$active_var.'"'; 
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)){
    if($given_password == $row['password']){
        $matched_username = $row['username'];
        $matched_password = $row['password'];
        }
    };

    # If there was a match 
    if($matched_username != "" && $matched_password != ""){
        #If there is only one result returned
        echo json_encode(array("$matched_password"));
        $session_sql = 'SELECT id, username, password, last_login FROM %table% WHERE username="'.$matched_username.'" AND password="'.$matched_password.'"';
        $session_result = mysql_query($session_sql);
        while($returned_row = mysql_fetch_assoc($session_result)){
            if($matched_password == $returned_row['password']){ 


            $_SESSION['id'] = $returned_row['id'];
            //$_SESSION['last_login'] = $returned_row['last_login'];
            $_SESSION['username'] = $returned_row['username'];
                }
             }

            $date = date('Y-m-d H:i:s');
            $update_sql = "UPDATE %table% SET last_login='".$date."'";
            mysql_query($update_sql);

        echo json_encode(array("success"=>"user logged in", "session"=>$_SESSION));
        }
    }

?>

此外,update_sql 正在更新我表中的每一行,这有点奇怪。不知道我哪里出错了有什么帮助吗?

编辑:

更新

   $.ajax({
        type: 'post',
        url: '<?php echo get_stylesheet_directory_uri(); ?>/connect.php',
        data: $('#login_form_header').serialize(),
        dataType: 'json',
        success: function(result){

                window.location = "my-dashboard/";

            };
        },
        error: function(e){console.log("Could not retrieve login information")}
    });
4

2 回答 2

0

试试这个:

success: function(result){ window.location = "my-dashboard/"; }

因为这个函数调用成功的结果。第一个参数是来自服务器答案的数据。这不是对象。您可以在那里找到详细信息:http: //api.jquery.com/jQuery.ajax/

于 2012-11-17T05:55:02.587 回答
0
success: function(result){
            if (result.success){
                window.location = "my-dashboard/";
                return false;
            };

改成

success: function(result){                    
                    window.location = "my-dashboard/";                     
                }

result这是服务器发送的数据,而不是对象。


update_sql 也在更新我表中的每一行有点奇怪

这是因为您的查询中没有WHERE子句。UPDATE

从文档中:

WHERE 子句(如果给定)指定标识要更新哪些行的条件。如果没有 WHERE 子句,所有行都会更新。

更多信息在这里

更新后编辑

你有一个额外的};

改变

success: function(result){

                window.location = "my-dashboard/";

            };
        },

success: function(result){

                window.location = "my-dashboard/";


        },

有几点值得一提:

  • 弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调将在 jQuery 1.8 中弃用。要为最终删除准备代码,请改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。

  • mysql_* 函数已被弃用。请改用 PDO。

于 2012-11-17T06:10:37.403 回答