2011-11-13 11 views
0

Howdy私はユーザー名とパスワードを持つログインフォームを持っています。PHP PDO :: MySQLとJQueryとAjax

私は、そのデータを正しく処理し、応答をjson_encodeするバックエンドPHPスクリプトを持っています。

フォームの検証を処理していないような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; 
      } 
     }); 
    }); 

アラートは常に起動します - "あなたは、このセクションにアセスを持っていない" 支援のための

感謝を。

答えて

1

あなたの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");     
      } 
    }); 
+0

私はこの問題を細かくしません。問題はPDOクラスとJQueryおよび/またはajaxにあると思います。私はajaxなしでフォームを処理する場合、それは正常に動作しますが、phpのsuthdataにはnullです。 –

+0

さて、あなたは正しかったが、今はデータをエコーし​​ているだけで、レスポンスコードを表示するだけでは成功関数に達していないのですが、非json準拠と思われます.... –