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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算