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 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题