<?php
if(isset($_POST[user_name]))
{
$user_name=$_POST[user_name];
include("include/conn.php");
$sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'")
or die(mysql_error());
//checking weather user exists or not in $existing_users array
if (mysql_num_rows($sql_check))
{ //user name is not availble
echo "no";
}
else
{
//user name is available
echo "yes";
}
}
?>
jQuery代碼用戶名可用的或者是不使用PHP的AJAX
<script language="javascript">
$(document).ready(function()
{
$("#username").blur(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the username exists or not from ajax
$.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
</script>
我「米做AJAX驗證用戶名存在與否但是這個代碼總是顯示可用即使用戶名存在於數據庫中。
使用這段代碼,我發送了沒有和是的jquery如果沒有用戶名不可用是然後可用
代碼有什麼問題??
你能後的JavaScript的呢? – 2011-03-27 05:07:32
定義了$ username嗎?您正在設置'$ user_name'的值。 – 2011-03-27 05:08:32
您應該引用數組的鍵:'$ _POST ['user_name']',更重要的是,您應該採取預防措施來防止SQL注入攻擊。例如,如果'$ user_name'將包含'1'; DROP TABLE註冊; - 你會失去你的桌子。 – 2011-03-27 05:22:09