我有这个代码用于实时用户名搜索,但它不起作用。非常新的ajax并且对PHP有一个好的把握...认为我是一个菜鸟......
这是我使用的脚本
<script>
$(document).ready(function(){
$("#uname").keyup(function() {
var name = $('#uname').val();
if(uname=="")
{
$("#disp").html("");
}
else
{
$.ajax({
type: "POST",
url: "getuser.php",
data: "name="+ uname ,
success: function(html){
$("#disp").html(html);
}
});
return false;
}
});
});
</script>
后面是表格
form name="registerationform" id="registerationform" method="post" autocomplete="off">
<input type="hidden" name="action" value="signup">
<div class="editProfileCont">
<div class="formFieldRow">
<div class="fields">
First Name<sup><font color="#FF0000">*</sup></font>: <input type="text" name="fname" maxlength="30" id="fname" class="required" minlength="1" value="<?php if(isset($_POST[fname])){ echo $_POST[fname];}?>"></div></div><br><br>
<div class="formFieldRow">
<div class="fields">
Last Name<sup><font color="#FF0000">*</sup></font>: <input type="text" name="lname" maxlength="30" id="lname" class="required" minlength="1" value="<?php if(isset($_POST[lname])){ echo $_POST[lname];}?>"></div></div> <br><br>
<div class="formFieldRow">
<div class="fields">
Age<sup><font color="#FF0000">*</sup></font>: <input type="text" name="age" maxlength="2" id="age" class="required" minlength="1" value="<?php if(isset($_POST[age])){ echo $_POST[age];}?>"></div></div><br>
<b><?php echo $errorEmail?><br></b>
<div class="formFieldRow">
<div class="fields">
E-Mail Id<sup><font color="#FF0000">*</sup></font>: <input type="text" name="email" maxlength="40" id="email" class="required email" minlength="1" value="<?php if(isset($_POST[email])){ echo $_POST[email];}?>"></div></div><br>
<b><?php echo $errorUname?><br></b>
<div class="formFieldRow">
<div class="fields">
User Name<sup><font color="#FF0000">*</sup></font>: <input type="text" name="uname" maxlength="10" id="uname" class="required" minlength="1" value="<?php if(isset($_POST[uname])){ echo $_POST[uname];}?>" onchange="showUser(this.value)"></div></div> <br>
<div id="disp"></div><br /><br>
<div class="formFieldRow">
<div class="fields">
Password<sup><font color="#FF0000">*</sup></font>: <input type="password" name="pword" id="pword" class="required" minlength="5"></div></div><br><br><br>
<input type="submit" value="Submit" class="commenButton submitResetBTN"> <input name="reset" type="reset" value="Reset" class="commenButton submitResetBTN">
now this is my getuser.php
<?php
session_start();
include "connection.php";
$q=$_POST['name'];
$sql="SELECT * FROM persons WHERE UserName = '".$q."'";
$result = mysqli_query($con,$sql);
$row=mysqli_num_rows($result);
if($row==0)
{
echo "<span style='color:green;'>Available</span>";
}
else
{
echo "<span style='color:red;'>Already exist</span>";
}
?>
它只是留下来。什么都没发生。该怎么办??帮助
答案 0 :(得分:0)
$(document).ready(function(){
$("#uname").keyup(function() {
var uname = $('#uname').val();
if(uname=="")
{
$("#disp").html("");
}
else
{
$.ajax({
type: "POST",
url: "getuser.php",
data: "name="+ uname ,
success: function(html){
$("#disp").html(html);
}
});
return false;
}
});
});
您的脚本说var name = ...
,然后在一行之后检查uname
。以上代码已更正。其余的代码对我来说是正确的。