duanche5149 2016-08-18 20:20
浏览 22
已采纳

用户在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!

  • 写回答

1条回答 默认 最新

  • douturan1807 2016-08-18 20:33
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部