2017-01-16 86 views
-1

我有一个表单中的字段,它需要一个'授权代码'并使用AJAX和PHP对数据库进行检查。我试图在表单上使用Bootstrap反馈来反馈给用户,如果代码错误或不正确。目前它不工作,并且没有错误,如果有人能指出这个问题,将不胜感激。PHP AJAX - 验证字段不显示错误/成功

形式:

<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Home</title> 
    <!-- Scripts --> 
    <script src="js/jquery-3.1.1.min.js"></script> 
    <script src="js/bootstrap.min.js"></script> 
    <script src="js/validator.js"></script> 
    <script src="js/validator.min.js"></script> 
    <!-- CSS --> 
    <link href="css/font-awesome.min.css" rel="stylesheet" type="text/css"> 
    <link href="css/bootstrap.css" rel="stylesheet"> 
</head> 

<body> 

<form id="auth_form" action="action.php" method="post"> 

    <div class="form-group has-feedback" name="auth_code" id="auth_code"> 
    <label for="auth_code" class="control-label"> 
    Authorisation Code</label> 
    <input class="form-control" id="auth_code_input" name="auth_code_input" type="password"> 
    <span class="form-control-feedback glyphicon" id="iconBad"></span> 
     <span id="auth_code_result"></span> 
    </div> 

    <div class="form-group"> 
    <div> 
     <button class="btn btn-info" name="submit" type="submit" id="submit">Submit</button> 
    </div> 
    </div> 

</form> 

<!-- AJAX --> 
<script type="text/javascript"> 
$(document).ready(function() { 
    var x_timer;  
    $("#auth_code_input").keyup(function (e){ 
     clearTimeout(x_timer); 
     var auth_code = $(this).val(); 
     x_timer = setTimeout(function(){ 
      check_auth_code_ajax(auth_code); 
     }, 1000); 
    }); 

function check_auth_code_ajax(auth_code){ 
    var $auth_code = $('#auth_code'); 
    $("#auth_code_result").html('<img src="img/ajax-loader.gif"/>'); 
    $.post('auth_code_checker.php', 
     {'auth_code_input':auth_code}, 
     function(data) { 
      // data will be JSON, including a status we can use 
      $("#auth_code_result").html(''); 
      showStatus(data.status, $auth_code); 
     }, 
     'json' // Expect JSON response from server 
    ); 
} 
</script> 

<!-- Sets field to error/cross or success/tick --> 
<script> 

function showStatus(status, $target) { 
    if (status === 'ok') { 
     $target.removeClass('has-error').addClass('has-success'); 
     $('.glyphicon', $target).removeClass('glyphicon-remove').addClass('glyphicon-ok'); 
    } else { 
     $target.removeClass('has-success').addClass('has-error'); 
     $('.glyphicon', $target).removeClass('glyphicon-ok').addClass('glyphicon-remove'); 
    } 
} 
</script> 

</body> 
</html> 

auth_code_checker.php

<?php 

include 'pdo_config.php'; 
try { 
    $conn = new PDO($dsn, $user, $pass, $opt); 

    $auth_code = $_POST["auth_code_input"]; 
    $stmt = $conn->prepare("SELECT * FROM tbl_instructors WHERE auth_code = :auth_code"); 
    $stmt->bindParam(':auth_code', $auth_code); 
    $stmt->execute(); 

    $exists = $stmt->fetchColumn(); 

    if ($exists > 0) { 
     $response['status'] = 'ok'; 
    } else { 
     $response['status'] = 'fail'; 
    } 

    header("Content-Type: application/json", true); 
    echo json_encode($response); 

catch(PDOException $e) { 
    echo "Error: " . $e->getMessage(); 
} 

$conn = null; 
+0

之前终止try{}块你尝试过单独运行你的PHP文件提供认证码? – Roljhon

+0

不,我会试试这个。 – sinesine

+0

一些明智的代码缩进将是一个好主意。它可以帮助我们阅读代码,更重要的是,它可以帮助您**调试您的代码** [快速浏览编码标准](http://www.php-fig.org/psr/psr-2/ )为了您自己的利益。您可能会被要求在几周/几个月内修改此代码 ,最后您会感谢我。 – RiggsFolly

回答

1

您需要编码catch(){}

<?php 

include 'pdo_config.php'; 
try { 
    $conn = new PDO($dsn, $user, $pass, $opt); 

    $auth_code = $_POST["auth_code_input"]; 
    $stmt = $conn->prepare("SELECT * FROM tbl_instructors WHERE auth_code = :auth_code"); 
    $stmt->bindParam(':auth_code', $auth_code); 
    $stmt->execute(); 

    $exists = $stmt->fetchColumn(); 

    if ($exists > 0) { 
     $response['status'] = 'ok'; 
    } else { 
     $response['status'] = 'fail'; 
    } 

    header("Content-Type: application/json", true); 
    echo json_encode($response); 
} // <-------------------------------------New line 
catch(PDOException $e) { 
    echo "Error: " . $e->getMessage(); 
} 

$conn = null; 
+0

嗨,我得到了PHP的错误,这解决了这个问题,谢谢,但是在这个领域还没有结果。 – sinesine

+0

@sinesine *“这就解决了这个问题”* - 为什么答案旁边没有绿色的勾号?已经2天了 –