检查mySQL数据库中是否存在记录模糊 [英] Check if record exists in mySQL Database on blur

查看:153
本文介绍了检查mySQL数据库中是否存在记录模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑如何实现这个场景:

我有一个表订单,其中有一个 serialNumber 字段。然后我也有一个表单的PHP页面。我想要做的是, onBlur 或者按下< input type = text> 字段,我想要一个ajax / jquery脚本来检查文本框输入的序列号,如果它在我的mySQL数据库中有一个现有的记录。然后,脚本会警告用户序列号已存在于数据库中,并且不允许提交。



我知道如何使用标准表单提交来实现它,但我是思考是否可以在没有按下提交按钮的情况下完成。



有没有一种方法可以实现这一点?

解决方案

为此,您可以使用javascript。在模糊事件上调用文本框的JavaScript上创建。
在这里我创建了函数,调用模糊事件上的文本框。

 函数CheckUserName(){
var UserName = document.getElementById('UserName');
if(UserName.value!=)
{
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为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 =用户名已存在请选择其他用户名;
UserName.focus();
}
else
{
document.getElementById('ErrorSpan')。innerHTML =;



xmlhttp.open(GET,checkUserName.php?q =+ UserName.value,true);
xmlhttp.send();
}
}

并创建一个名为checkusername.php的php文件并通过查询字符串传递您的值。



php代码如下。

 <?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);

?>

如果用户名发现它会返回值,您可以在javascript函数中获得值。我希望它能帮助你。


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?

解决方案

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.

这篇关于检查mySQL数据库中是否存在记录模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆