เวลาลงทะเบียนมันไม่เข้าฐานข้อมูล ตอนแรกทำได้ครับ แต่พอเอา jquery มาแทรกเพื่อ check user มันก็ไม่เข้าครับ
ไฟล์ คอนเน็ตเชืื่อมต่อได้ปกติครับ
register.php
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com Tutorials</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()//When the dom is ready
{
$("#username").change(function()
{ //if theres a change in the username textbox
var username = $("#username").val();//Get the value in the username textbox
if(username.length > 3)//if the lenght greater than 3 characters
{
$("#availability_status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "ajax_check_username.php", //file name
data: "username="+ username, //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="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="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
}
});
}
});
}
else
{
$("#availability_status").html('<font color="#cc0000">Username too short</font>');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});
</script>
<style type="text/css">
body {
font-family:Arial, Helvetica, sans-serif
}
#availability_status {
font-size:11px;
margin-left:10px;
}
input.form_element {
width: 221px;
background: transparent url('bg.jpg') no-repeat;
color : #747862;
height:20px;
border:0;
padding:4px 8px;
margin-bottom:0px;
}
label {
width: 200px;
float: left;
text-align: left;
margin-right: 0.5em;
display: block;
}
.style_form {
margin:3px;
}
#content {
margin-left: auto;
margin-right: auto;
width: 600px;
margin-top:200px;
}
#submit_btn {
margin-left:133px;
height:30px;
width: 221px;
}
</style>
<body>
<div id="content">
<form method="post" action="savemember.php">
</form>
<div id="content">
<form action="savemember.php" method="post">
<div class="style_form">
<label for="username">Username :</label>
<input type="text" name="username" id="username" class="form_element"/>
<span id="availability_status"></span> </div>
<div class="style_form">
<label for="Password">Password :</label>
<input name="txtPassword" type="password" id="username" class="form_element">
</div>
<div class="style_form">
<label for="Confirm Password">Confirm Password :</label>
<input name="txtConPassword" type="password" id="txtConPassword" class="form_element">
</div>
<div class="style_form">
<label for="Name">Name :</label>
<input name="txtName" type="text" id="txtName" class="form_element">
</div>
<div class="style_form">
<label for="Email">Email :</label>
<input name="txtEmail" type="text" id="txtEmail" class="form_element">
</div>
<br>
<input type="submit" name="Submit" value="Save">
</form>
</div>
</div>
</body>
</html>
savemember.php
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Live Username Check on registration form Using PHP/jQuery</title>
</head>
<body>
<?
include('database_connection.php');
session_start();
//Include The Database Connection File
if(isset($_POST['username']))//If a username has been submitted
{
$username = mysql_real_escape_string($_POST['username']);//Some clean up :)
$check_for_username = mysql_query("SELECT userid FROM member WHERE Username='$username'");
//Query to check if username is available or not
if(mysql_num_rows($check_for_username))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - Username is available
}
}
if(trim($_POST["username"]) == "")
{
echo "Please input Username!";
exit();
}
if(trim($_POST["txtPassword"]) == "")
{
echo "Please input Password!";
exit();
}
if($_POST["txtPassword"] != $_POST["txtConPassword"])
{
echo "Password not Match!";
exit();
}
if(trim($_POST["txtName"]) == "")
{
echo "Please input Name!";
exit();
}
if(trim($_POST["txtEmail"]) == "")
{
echo "Please input Email!";
exit();
}
$strSQL = "SELECT * FROM member WHERE Username = '".trim($_POST['username'])."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
echo "Username already exists!";
echo "<a href='index.php'>กลับไปหน้าหลัก</a>";
}
else
{
$strSQL = "INSERT INTO member (Username,Password,Name,Email,Status,SID,Active) VALUES ('".$_POST["username"]."',
'".$_POST["txtPassword"]."','".$_POST["txtName"]."' ,'".$_POST["txtEmail"]."','USER','".session_id()."')";
}
mysql_close();
?>
</body>
$strSQL = "INSERT INTO member (Username,Password,Name,Email,Status,SID,Active) VALUES ('".$_POST["username"]."',
'".$_POST["txtPassword"]."','".$_POST["txtName"]."' ,'".$_POST["txtEmail"]."','USER','".session_id()."')";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Live Username Check on registration form Using PHP/jQuery</title>
</head>
<body>
<?
include('database_connection.php');
session_start();
//Include The Database Connection File
if(isset($_POST['username']))//If a username has been submitted
{
$username = mysql_real_escape_string($_POST['username']);//Some clean up :)
$check_for_username = mysql_query("SELECT userid FROM member WHERE Username='$username'");
//Query to check if username is available or not
if(mysql_num_rows($check_for_username))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - Username is available
}
}
if(trim($_POST["username"]) == "")
{
echo "Please input Username!";
exit();
}
if(trim($_POST["txtPassword"]) == "")
{
echo "Please input Password!";
exit();
}
if($_POST["txtPassword"] != $_POST["txtConPassword"])
{
echo "Password not Match!";
exit();
}
if(trim($_POST["txtName"]) == "")
{
echo "Please input Name!";
exit();
}
if(trim($_POST["txtEmail"]) == "")
{
echo "Please input Email!";
exit();
}
$strSQL = "SELECT * FROM member WHERE Username = '".trim($_POST['username'])."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
echo "Username already exists!";
echo "<a href='index.php'>กลับไปหน้าหลัก</a>";
}
else
{
$strSQL = "INSERT INTO member (Username,Password,Name,Email,Status,SID,Active) VALUES ('".$_POST["username"]."',
'".$_POST["txtPassword"]."','".$_POST["txtName"]."' ,'".$_POST["txtEmail"]."','USER','".session_id()."')";
mysql_query($strSQL);
echo "ลงทะเบียนเสร็จสมบูรณ์แล้ว ";
}
mysql_close();
?>
</body>