<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Fancy Style Username availability Checking using jQuery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">
//<!---------------------------------+
// Developed by Roshan Bhattarai
// Visit http://roshanbh.com.np for this script and more.
// This notice MUST stay intact for legal use
// --------------------------------->
$(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>
</style>
</head>
<body>
<br>
<br>
<div align="center">
<div class="top" > A fancy username availability checker using jQuery - <strong>Username "roshan", "mike" ,"jason" already exists</strong><br>
Please move the focus out of the box to check the availability of username
</div>
<div >
User Name : <input name="username" type="text" id="username" value="" maxlength="15" />
<span id="msgbox" style="display:none"></span>
</div>
</div>
</body>
</html>
user_availability.php
<?php
// Developed by Roshan Bhattarai
// Visit http://roshanbh.com.np for this script and more.
// This notice MUST stay intact for legal use
//this varible contains the array of existing users
header("Content-Type:text/plain;charset=TIS-620");
$objConnect = mysql_connect("localhost","root","admin") or die("Error Connect to Database");
$objDB = mysql_select_db("abc");
//*** Check Username (already exists) ***//
$strSql="select * from account ";
$query = mysql_query($strSql);
$num = mysql_num_rows($query);
$resultArray = array();
for ($i = 0;$i<$num;$i++) {
$result = mysql_fetch_array($query);
array_push($resultArray,$result);
}
//$existing_users=array('roshan','mike','jason','อา.'); [font color='red']ถ้าเป็นแบบนี้อ่ะได้ครับ เพราะเป็น array[/font]
$existing_users=($result);
//value got from the get metho
$user_name=$_POST['user_name'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
//user name is not availble
echo "no";
}
else
{
//user name is available
echo "yes";
}
?>
แล้วอยากได้ฐานข้อมูลจาก Mysql มาเป็น array โดยที่
record 1 = array ตัวที่ 1
record 2 = array ตัวที่ 2 ประมาณนี้อ่ะครับ