这是从数据库AJAX获取数据的代码我可以查看表名和语法但是有一些问题我无法获取数据。
文件名:: ajax_check_uname.php
<?php
$dbhandle = mysql_connect('localhost', 'root', 'root') or die("Opps some thing went wrong");
mysql_select_db('db_new', $dbhandle) or die("Opps some thing went wrong");
if(isset($_POST['uname']))//If a name has been submitted
{
$uname= mysql_real_escape_string($_POST['uname']);//Some clean up :)
$check_for_uname = mysql_query("SELECT uname FROM `db_new`.`operators` WHERE uname='$uname'");
//Query to check if uname is available or not
if(mysql_num_rows($check_for_uname))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - uname is available
}
}
mysql_close($dbhandle);
?>
这是脚本代码,我将调用上述文件进行数据验证。
<script type="text/javascript">
$(document).ready(function() //When the dom is ready
{
$("#uname").change(function()
{ //if theres a change in the uname textbox
var uname = $("#uname").val(); //Get the value in the uname textbox
//var re = ^([\w-]+(?:\.[\w-]+)*)\$;
if(uname) //if the length greater than 5 characters
{
$("#availability_status").html('<img src="/ahs/images/loader.gif" align="absmiddle"> Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "/abc/app/views/ControlPanelViews/forms/ajax_check_uname.php", //file name
data: "uname="+ uname, //data
//data: {uname: value},
success: function(server_response){
$("#availability_status").ajaxComplete(function(event, request){
if(server_response == '0') //if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="/abc/images/available.png" align="absmiddle"> <font color="Green"> Available </font> ');
//add this image to the span with id "availability_status"
}
else if(server_response == '1') //if it returns "1"
{
$("#availability_status").html('<img src="/abc/images/not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
}
});
}
});
}
else
{
$("#availability_status").html('<label></label>');
//if in case the uname is less than or equal 6 characters only
}
return false;
});
});
</script>
以下是用于显示数据的HTML表单代码。
HTML表格:
<div class="form-group">
<label class="control-label col-md-3">Username</label>
<div class="col-md-9">
<input type="text" name="uname" id="uname" class="form-control" placeholder="name" type="text">
<span id="availability_status"></span>
</div></div>
答案 0 :(得分:0)
<script type="text/javascript">
$(document).ready(function() //When the dom is ready
{
$("#uname").change(function()
{ //if theres a change in the uname textbox
var uname = $("#uname").val(); //Get the value in the uname textbox
//var re = ^([\w-]+(?:\.[\w-]+)*)\$;
if(uname) //if the length greater than 5 characters
{
$("#availability_status").html('<img src="/ahs/images/loader.gif" align="absmiddle"> Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "/abc/app/views/ControlPanelViews/forms/ajax_check_uname.php", //file name
data: {uname: uname}, //data
success: function(server_response){
$("#availability_status").ajaxComplete(function(event, request){
if(server_response == '0') //if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="/abc/images/available.png" align="absmiddle"> <font color="Green"> Available </font> ');
//add this image to the span with id "availability_status"
}
else if(server_response == '1') //if it returns "1"
{
$("#availability_status").html('<img src="/abc/images/not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
}
});
}
});
}
else
{
$("#availability_status").html('<label></label>');
//if in case the uname is less than or equal 6 characters only
}
return false;
});
});
</script>