I have a form that requires 'authorization' - where the user needs to enter one of the authorization codes stored in a database to be able to submit the form (basically a password). I'm trying to use AJAX with PHP to both display (using Bootstrap's feedback glyphicons) a tick or cross depending on whether or not the user entered a valid value and allow or prevent form submission. If the user did not enter a valid value, form submission is prevented.
My code below currently isn't working, any help would be greatly appreciated.
HTML:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
<form name="auth_form" id="auth_form" method="post" action="action.php">
    <p>Please enter authorisation code.</p>
    <div class="form-group has-feedback" name="auth_code" id="auth_code">
        <input class="form-control" id="auth_code_input" autocomplete="new-password" name="auth_code_input" type="password" required>
        <span class="form-control-feedback glyphicon" id="statusIcon"></span>
    </div>
    <button class="btn btn-success btn-ok" name="Submit" id="submit" type="Submit">Submit</button>
</form>
JS:
<script>
    $(document).ready(function() {
        // Validate on blur and display 'cross' icon in span if invalid, tick if valid
        $('#auth_code_input').blur(function() {
            if (!ValidateInput()) {
                e.preventDefault();
            }
        });
        // Validate on submit and prevent form submission if invalid
        $('#auth_form').on('submit', function(e) {
            if (!ValidateInput()) {
                e.preventDefault();
            }
        })
    });
    // AJAX to check the auth-code server-side
    function ValidateInput() {
                var given_code = document.getElementById('auth_code_input').value;
                $.ajax({
                        url: 'checkauth.php',
                        type: 'POST',
                        data: given_code
                    });
                .done(function(response) {
                            var response = valid;
                            if valid = '1' {
                                $('#statusIcon').removeClass('glyphicon-remove').addClass('glyphicon-ok');
                                IsValid = true;
                            } else {
                                $('#statusIcon').removeClass('glyphicon-ok').addClass('glyphicon-remove');
                                IsValid = false;
                            }
                        }
                .fail(function() {
                            IsValid = false;
                            $('#auth_code_input').val('Something went wrong, please try again.');
                });
            return IsValid;
        });
</script>
checkauth.php
<?php
error_reporting(E_ALL);
ini_set( 'display_errors', 1);
$given_code = $_REQUEST['given_code'];
include 'pdo_config.php';
$valid = '0';
try {
    $conn = new PDO($dsn, $user, $pass, $opt);
    $stmt = $conn->prepare("SELECT instructor_id, auth_code FROM tbl_instructors WHERE auth_code = :given_code;");
    $stmt->bindParam(':given_code', $given_code);
    $stmt->execute();
    $row = $stmt->fetchColumn();
    if ($row == 1) { // row equals one, means auth-code corresponds to an instructor and is valid
        $valid = '1';
    } else {
        $valid = '0';
    }
    echo valid;
}
catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
 
     
    