在模糊的情况下检查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?
我正在考虑如何实现这种情况: p>
我有一个 table 我知道如何使用标准表单提交来实现它,但我想它是否可以 没有文字按下提交按钮就可以完成。 p>
有没有办法实现这个? p>
div> orders code>,其中有
serialNumber code>字段。 然后我还有一个带有表单的PHP页面。 我在想的是,
onBlur code>或者在按键上输入/返回
&lt; input type = text&gt; code>字段,我想要一个ajax / jquery脚本来 如果我的mySQL数据库中有现有记录,请从文本框输入中检查序列号。 然后脚本会警告用户序列号已经存在于数据库中,并且不允许提交。 p>
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.
you can also use this method I checked it on keyup you can use it via onblur option.
<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....