JQuery没有给出反馈

时间:2016-03-02 19:59:01

标签: php jquery html twitter-bootstrap

我有一个小型的学校项目。我正在尝试检查是否在MySQL数据库中注册了赌场名称。如果名称不存在,则应从引导框架中显示小绿色标记(glyphicon-ok)。当我现在在表单中输入名称时,我根本没有得到任何来自我的JQuery的反馈。所以我的具体问题是:

  • 我的JQuery中的语法是否正确?
  • 使用相同的" createcasino"是不正确的输入类型中的id和name?

我希望有人可以帮助我?

HTML:

<form id="casinoForm" method="post" class="form-horizontal fv-form fv-form-bootstrap" action="createcasino.php" novalidate="novalidate"><button type="submit" class="fv-hidden-submit" style="display: none; width: 0px; height: 0px;"></button>
    <div class="form-group has-feedback">
       <label class="col-sm-3 control-label">Casino Name</label>
       <div class="col-sm-5">
          <input type="text" class="form-control" name="createcasino" id="createcasino" data-fv-field="createcasino"><i class="form-control-feedback" data-fv-icon-for="createcasino" style="display: none;"></i>
          <small class="help-block" data-fv-validator="notEmpty" data-fv-for="createcasino" data-fv-result="NOT_VALIDATED" style="display: none;">The Casinoname is required</small><span id="casinocheck" class="help-block"></span</div>
   </div>
</form>

createvalidation.js

    $(document).ready(function(){
    $('#createcasino').keyup(function() {
    var usercheck = $(this).val();
            $('#casinocheck').html('');                     /* !!!!Koden finder ikke glyphicon !!!!!*/
            $.post("casinocheck.php", {casino_name: usercheck } , function(data)
            {
            if (data.status == true)
            {
            $('#casinocheck').parent('div').removeClass('has-error').addClass('has-success');

            } else {
            $('#casinocheck').parent('div').removeClass('has-success').addClass('has-error');
            }
            $('#casinocheck').html(data.msg);
            },'json');
    });
});


$(document).ready(function() {
    $('#casinoForm').formValidation({
        message: 'test',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        createcasino: {
                message: 'test',
                validators: {
                    notEmpty: {
                        message: 'The Casino name is required'
                    }
                }    
        }  
    )};
)};

casinocheck.php

    <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include 'dbconfic.inc.php';

if(isset($_POST['casino_name']) && $_POST['casino_name'] != '')
    {
        $response = array();
        $cas = mysqli_real_escape_string($mysqli,$_POST['casino_name']);
        $sql  = "SELECT * FROM casinos WHERE name='' ";
        $res    = mysqli_query($mysqli, $sql);
        $count  = mysqli_num_rows($res);
        if($count > 0)
        {
            $response['status'] = false;
            $response['msg'] = 'Casino Already Exist.';
        }
        else
        {
            $response['status'] = true;
            $response['msg'] = 'Casino available.';
        }
         echo json_encode($response);
}?>

createcasino.php

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


// Selecting Database
include 'dbconfic.inc.php';

if(isset($_POST["createcasino"])) {
    $cas = trim($_POST["createcasino"]);


    $stmt = $mysqli->prepare("INSERT INTO casinos(name) VALUES(?)");

        $stmt->bind_param('s', $cas);

            // execute prepared statement 
                if ($stmt->execute()) { // tjek om udført:
                    $success = true;
                }
                    // luk statement
                $stmt->close();

                // luk connection
                $mysqli->close();   
if($success) {
    echo "Insert Succesfull";
} else {
    echo "Failed" .  $stmt->error;
  }

}

?>

1 个答案:

答案 0 :(得分:1)

您正在寻找表单ID而不是keypress id

textbox事件
$('#casinoForm').keyup(function() { // this id has to be your input box id and not your form id

createcasino事件使用keyup。然后它将从文本框中捕获数据。

<input type="text" class="form-control" name="createcasino" id="createcasino" data-fv-field="createcasino">

因此请将其更改为:$('#createcasino').keyup(function() {

这也必须改为:

$.post("casinocheck.php", {casino_name: usercheck } , function(data)

您还缺少选择查询中的位置:

$sql  = "SELECT name FROM casinos";

检查此jsfiddle https://jsfiddle.net/bsgxj5od/