duanche5149 2016-08-19 04: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-19 04: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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应