doudilin1225 2012-09-11 10:41
浏览 48
已采纳

在模糊的情况下检查mySQL数据库中是否存在记录

I am thinking on how to implement this scenario:

I have a table orders where there is a serialNumber field. Then I also a have a PHP page with a form. What I am thinking of doing is that, onBlur or on keypress enter/return of a <input type=text> field, I would like an ajax/jquery script to check the serial number from the text box input if it has an existing record in my mySQL database. Then the script will warn the user that the serial number exists already in the database and will not allow submission.

I know how to implement it with standard form submission but I was thinking is it can be done without the literal pressing of the submit button.

Is there a way to implement this?

  • 写回答

2条回答 默认 最新

  • dongxian6653 2012-09-11 11:03
    关注

    for this you can use javascript. create on javascript that called on textbox on blur event. here i created on function that called on textbox on blur event.

    function CheckUserName(){
        var UserName = document.getElementById('UserName');
        if(UserName.value != "")
        {
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    var value = xmlhttp.responseText;
                    if(value.length > 1)
                    {
                        document.getElementById('ErrorSpan').innerHTML="User Name Already exist please choose Other User Name";
                        UserName.focus();
                    }
                    else
                    {
                        document.getElementById('ErrorSpan').innerHTML="";
                    }
                }
              }
            xmlhttp.open("GET","checkUserName.php?q="+UserName.value,true);
            xmlhttp.send();
        }
    }
    

    and create one php file with the name checkusername.php and pass your value through query string.

    php code as follow.

    <?php
    $q=$_GET["q"];
    include("db_connect.php");
    
    $sql="select * from usermaster where UserName='".$q."'";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
      {
        echo $row['UserName'];
      }
    mysql_close($con);
    
    ?>
    

    here from php if username find it will return value and you can get value in your javascript function. i hope it will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)