用户在PHP中键入重复键时自动显示文本

用户在PHP中键入重复键时自动显示文本

问题描述:

I need some help with my project, when i type in textbox (primary key - textbox), if it is duplicated key in mysql, it will show text next to textbox like this (not click button, it will show auto when i type: ABC12444 -> this record is exist in system):

enter image description here

My code:

 $idcustomer = $_POST['txtIDCus'];
 $result = $mysqli->query("SELECT * FROM customer where id='$idcustomer' ");
    if ($result->num_rows > 0) {
        $errMsg="Key exist in system";
    }

Simple HTML Code

<b>ID Customer:</b>
<input type="text" id="txtIDCus" name="txtIDCus" width="100" placeholder="Type ID Customer" size="40" class="form-control" maxlength="70"/> <br />

I try to show my stuck, hope you help me. Thanks!

You can do this using jquery.. I hope this helps..

HTML

<input type="text" id="key" /> <span id="key-result"> </span>

JavaScript

$("#key").on("input", function() {
        var key = this.value;
        //you can skip using timeout just call check_key_ajax(key);
        x_timer = setTimeout(function(){check_key_ajax(key)}, 1000);            
    });

function check_key_ajax(key){            
        $.post('checkKey.php', {'Key':key}, function(data) {$("#key-result").html(data);});            
    }

Checker.php

    if(isset($_POST["Key"]))
    {
    $idcustomer = $_POST["Key"];
    $result = $mysqli->query("SELECT * FROM customer where id='$idcustomer'");
    if ($result->num_rows > 0) {
        $errMsg="Key exist in system";
        echo $errMsg;
    }
}