duanpi7578 2016-07-08 15:03
浏览 265
已采纳

如何验证输入以查看它是否与数据库中的记录匹配

I am trying to create an input that queries the database and returns whether or not a result exists in the database. I have it partially working, but my box is glowing green whenever I only type in one letter. It would be better if it stayed red until it actually found a exact match and then turned green. Edit: I just realized there is also something wrong with my query. It is correctly querying the database now. The original issue is my main problem.

$(document).ready(function(){
    $("#load").keyup(function (e){
        e.preventDefault();
       ;

    searchRequest = $.ajax({
        url: 'check_load_no.php',
        data: $('#load').serialize(),
        type: 'POST',
        success: function (data) {
            $(".verify").css('box-shadow', '0px 0px 9px 2px #84f850');
            $(".error").css('display', 'none');
            $(".success").css('display', 'block');

        },
        error: function (data) {
            $(".verify").css('box-shadow', '0px 0px 9px 2px #ad0037');
            $(".success").css('display', 'none');
            $(".error").css('display', 'block');
        }

    });

    });
   });

Below is my php

<?php include('../model/conn.php'); ?>
<?php include('../model/conn2.php') ?>

<?php
$sql = "SELECT cmt_2 FROM oeordhdr_sql WHERE cmt_2 = '{$_POST['load']}'";
$query = (odbc_exec($conn,$sql));
$row = (odbc_fetch_row($query));
if($row['cmt_2']){
    echo 'yeah';
}

HTML

  <h1>Please add the info based on your load number</h1>
        <form action="" method="post">
        <div class="card" >
            <input class="verify" id="load" type="text" name="load" placeholder="Load Number" required/>
    <span class="error" style="display: none;"><i class="fa fa-exclamation-triangle fa-lg" aria-hidden="true"> </i>I'm not finding anything</span>   
    <span class="success" style="display: none;"> <i class="fa fa-check-cube fa-lg" aria-hidden="true"> </i> Congratulations, that record exists!</span><br> 
<button  class="update_button" type="submit" name="add" value="update">Update</button></div></form>
  • 写回答

3条回答 默认 最新

  • douxiang3978 2016-07-08 16:53
    关注

    Decided to output the error/success message using php instead of changing css

    $("#load").keyup(function (e){
            e.preventDefault();
    
    
        searchRequest = $.ajax({
            url: 'check_load_no.php',
            data: $('#load').serialize(),
            type: 'POST',
            success: function (data) {
                console.log(data);
                if(data==="yeah")
                {
                    $(".validate").html(data);
    
                }
                else
                {
                    $(".validate").html(data);
    
    
                }
            }
    
        });
    
        });
    

    My php

     $sql = "SELECT cmt_2 FROM oeordhdr_sql WHERE cmt_2 LIKE '{$_POST['load']}'";
        $query = odbc_exec($conn,$sql);
        $row = (odbc_fetch_row($query));
        if($row){
            echo '<span class="success" style="display: block;"> <i class="fa fa-check-cube fa-lg" aria-hidden="true"> </i> Congratulations, that record exists!</span>';
    
        }else{
            echo'<span class="error" style="display: block;"><i class="fa fa-exclamation-triangle fa-lg" aria-hidden="true"> </i>I\'m not finding anything</span>';
        }
    

    My HTML

    <h1>Please add the info based on your load number</h1>
            <form action="" method="post">
            <div class="card" >
                <input class="verify" id="load" type="text" name="load" placeholder="Load Number" required/>
        <div class="validate"></div><br> 
    <button  class="update_button" type="submit" name="add" value="update">Update</button></div></form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 鸿业暖通修改详细负荷时闪退
  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体