dsjhejw3232 2017-01-16 19:56
浏览 56

PHP AJAX - 验证字段不显示错误/成功[关闭]

I have a field in a form that takes in an 'authorisation code' and checks it against a database using AJAX and PHP. I'm trying to make use of the Bootstrap feedback on forms to feedback to the user if the code was wrong or not. Currently it's not working, and there are no errors, if anyone could point out the issue it would be greatly appreciated.

form:

<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;
  • 写回答

1条回答 默认 最新

  • drymoeuka282427675 2017-01-16 20:12
    关注

    You need to terminate the try{} block before coding the catch(){} block

    <?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;
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)