简体   繁体   English

检查记录是否模糊存在于mySQL数据库中

[英]Check if record exists in mySQL Database on blur

I am thinking on how to implement this scenario: 我正在考虑如何实现这种情况:

I have a table orders where there is a serialNumber field. 我有一个表orders ,其中有一个serialNumber字段。 Then I also a have a PHP page with a form. 然后,我还有一个带有表单的PHP页面。 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. 我想做的是,在onBlur或按键输入/返回<input type=text>字段时,我希望ajax / jquery脚本从文本框输入中检查序列号(如果它具有现有记录)在我的mySQL数据库中。 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. 为此,您可以使用javascript。 create on javascript that called on textbox on blur event. 在模糊事件上在文本框上调用的javascript上创建。 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. 并创建一个名称为checkusername.php的php文件,并通过查询字符串传递您的值。

php code as follow. 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);

?>

here from php if username find it will return value and you can get value in your javascript function. 如果用户名找到它,它将从php返回值,您可以在javascript函数中获取值。 i hope it will help you. 希望对您有帮助。

you can also use this method I checked it on keyup you can use it via onblur option. 您也可以使用此方法我在keyup上选中它,可以通过onblur选项使用它。

  <input  type="text"  value="" id="jform_domain_name" name="jform[domain_name]"   onkeyup="checkAvailability();"/>

<div id="errormsg" class="no-display">Sorry, this name is not available!</div>
<div id="successmsg" class="no-display">Congratulations, this domain is available!</div>
 <script>
 function checkAvailability(){

    jQuery('#ajaxloader').html('<img src="images/ajax-loader.gif" />');
    var string = jQuery('#jform_domain_name').val();
    if(string == ''){
        jQuery('#ajaxloader').html('');
        return false;   
    }


    jQuery.ajax({
            type : "POST"
            ,url : "YOUR_ACTION_URL"
            ,data :"string="+jQuery('#jform_domain_name').val()

            ,success : function(data){               
                if(data==0){
                    var errormsg = jQuery("#errormsg").html();
                    jQuery("#ajaxloader").show();
                jQuery('#ajaxloader').html(errormsg);
            }else{
                var successmsg = jQuery("#successmsg").html();
                jQuery("#ajaxloader").show();
                jQuery('#ajaxloader').html(successmsg);
            }

        }
    ,complete : function(){

         if( jQuery('#jform_domain_name').val() == "" ) {
                jQuery("#ajaxloader").hide();
            }
        }
            ,beforeSend: function(html){
                jQuery("#ajaxloader").show();
                jQuery('#ajaxloader').html('<img style="padding-top:6px;" src="images/ajax-loader.gif" />');
            return;
        }
        });
}
 </script>

for reference I am providing my controller action and the model which I have used 供参考,我提供了我的控制器动作和所使用的模型

//sample controller action 
 function checkdomain(){
       $requestData = JRequest::get();
    $return = $model->checkAvailabiLity($requestData['string']);

    if($return === false){
        echo 0;
    }else{
        echo 1;
    }
    die;
} 
//sample model on which I created Query logic.
   public function checkAvailabiLity($data){
    $select = "SELECT id FROM #__jshopping_vendors WHERE domain_name = '".strtolower($data)."' AND user_id != ".$user->id."";
    $db->setQuery($select);
    $type = $db->loadObject();

    if(isset($type->id) && $type->id >0){
        return false;
    }else{
        return true;
    }
}

hope this helps.... 希望这可以帮助....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM