PHP PDO::MySQL 和 JQuery 和 Ajax

标签 php jquery mysql ajax pdo

你好,我有一个包含用户名和密码的登录表单。

我有一个后端 PHP 脚本,可以正确处理该数据并对响应进行 json_encode。

我有一个使用 Ajax 的 JQuery JS 脚本,它似乎无法处理表单验证,但似乎有一些东西泄漏,导致 ajax 阻塞。

我希望能对正在发生的问题有一些见解。

       <?php
    #
    #       General purpose script to verify user login
    #       Will be combined with jquery/AJAX to allow access based on
    #       role
    #
    set_include_path(get_include_path() . PATH_SEPARATOR . "/home/best9721/public_html/lib");
    include("t_const.php");
    include("t_verify.php");
    #
    #       Check that there is no SESSION Variables
    #
    if(isset($_SESSION)) {
        session_destroy();
    }
    #
    #       Cleanup POST variables
    #
            $username = strip_tags($_POST['username']);
            $userpass = strip_tags($_POST['password']);
    #
    #       Connect to DB
    #
    try {
            $dbh = new PDO("mysql:host=localhost;dbname=$DB_TEST", $MASTER_TEST, $MASTER_TEST_PSW);
    #
    #       Check and see if inputted username is in the DB to start with
    #
            $stmt = $dbh->prepare("SELECT * FROM user_auth where userid = :userid");
            $stmt->execute(  array (
                                    ':userid' => $username,
                                   )
                           );
            $authdata = $stmt->fetch(PDO::FETCH_ASSOC);
            if(empty($authdata)) {
               $response['error'] = true;
               $response['msg'] = "You do not have access to this section";
               print json_encode($response);
               exit;
            }
    #
    #       Check and see if they have access
    #
            $stmt = $dbh->prepare("SELECT auth_level FROM user_access where userid = :userid");
            $stmt->execute(  array (
                                ':userid' => $username,
                                  )
                          );
            $role = $stmt->fetchAll(PDO::FETCH_COLUMN);
            $auth_role = $_POST{'auth'};
            if(!has_access($role, $auth_role) or !isset($role)) {
                  $response['error'] = true;
                  $response['msg'] = "You do not have privileges for this section.";
                  print json_encode($response);
                  exit;
            } else {
                   $response['url'] = $url[$auth_role];
            }
    #
    #               Now check and see if their account is locked
    #
            if( $authdata['account_status'] == "closed") {
                   $response['error'] = true;
                   $response['msg'] = $authdata["reason_acct_locked"];
                   print json_encode($response);
                   exit;
             }
    #
    #               Check if Passwords match - final check
    #
             if(sha1($_POST['password']) != $authdata['userpsw']) {
                  $response['error'] = true;
                  $response['msg'] = "Invalid User Credentials";
                  print json_encode($response);
                  exit;
             } else {
                  $response['msg'] = 'OK';
                  print json_encode($response);
                  exit;
             }
    }
    #
    #       There was an error
    #
    catch(PDOException $e)
    {
        $response['error'] = true;
        $response['msg'] = $e->getMessage();
        print json_encode($response);
        exit;
    }
    ?>

和 auth_user.js

        $(document).ready(function() {
       $("#loginForm").validate({
           errorLabelContainer: "#errorMessage",
           wrapper: "li",
           rules: {
                  username: "required",
                  password: "required"
           },
           messages: {
                  username: "Please enter your username.",
                  password: "Please enter your password."
           },
           submitHandler: function() {
                 $.ajax({
                    type: 'POST',
                    url: 'auth_user.php',
               dataType: 'json',
                success: function(data) {
                             alert(data.msg);
                       },
                  error: function() {
                             alert("There was a problem processing this form");                
                       }
                      });
                      return false;
           }
       });
    });

~

始终弹出警报 - “您无权访问此部分”

感谢您的帮助。

最佳答案

在您的 ajax 调用中,您不会向服务器发送任何数据。您可以使用传递的对象中的数据属性来发送它:

 $.ajax({
           type: 'POST',
           url: 'auth_user.php',
           dataType: 'json',
           // data attribute
           data : {"username":"myUsername", "password": "myPassord"},
           // **
           success: function(data) {
                     alert(data.msg);
           },
           error: function() {
                      alert("There was a problem processing this form");                
           }
  });

关于PHP PDO::MySQL 和 JQuery 和 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8113826/

相关文章:

php - 使用 PHP 将 CSV 导入 MySQL

php - 将 MySQL 中的数据显示到 bootstrap 模式中(无 ajax)

php - 在 Laravel 测试期间创建和删除表

javascript - 在这种情况下,是否有替代全局变量的良好做法?

javascript - Counter-Up - 从两个零开始计数

javascript - 我想在我的聊天系统中使用回车键发送消息,并使用 shift+enter 键换行

使用 OR 运算符时 MySQL 查询变慢

MYSQL从查找中位出生日期输出中位年龄

php - 如何使用 PHP 和 MySQL 显示按归档分组的报告?

php - nusoap 简单服务器