หาโค๊ดในเว็บนี้แล้วมาประยุกต์ ใช้ ajax
Code
<script>
function send()
{
var ajaxRequest;
if(window.XMLHttpRequest)
{
ajaxRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert ("Browser error");
return false;
}
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 3)
{
var area = document.getElementById('area');
}
if(ajaxRequest.readyState == 4)
{
var area = document.getElementById('area');
area.innerHTML = ajaxRequest.responseText;
// area.innerHTML = " ";
}
}
var name = document.myForm.username.value;
ajaxRequest.open( "GET", "check_regis.php?"+"name="+encodeURIComponent(name), true);
ajaxRequest.send(null);
}
</script>
ในฟอร์ม
Code
<input name="username" id="username" type="text" />
<input type="button" value="check user" onclick ="send()">
<span id ="area"></span>
check_regis.php
Code
<?
$username = $_POST["username"];
$password = $_POST["password"];
$password2 = $_POST["password2"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$company = $_POST["company"];
$gender = $_POST["gender"];
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];
$email = $_POST["email"];
$address = $_POST["address"];
$country = $_POST["country"];
$postal_code = $_POST["postal_code"];
$tel = $_POST["tel"];
$fax = $_POST["fax"];
$mobile = $_POST["mobile"];
$favourite_color = $_POST["favourite_color"];
$username="select * from tbregister where username='$username'";
$resultuser=mysql_query($username);
$numuser=mysql_num_rows($resultuser);
if($numuser>0){
echo "usernameนี้ได้ถูกใช้งานแล้ว";
อยากให้กดเช็คแล้วมีข้อความหลังปุ่มว่ามีหรือไม่
// ช่วงนี้มึนๆมาก ช่วยทีนะคะ
}
else{
$sql = <<<SQL
insert into tbregister values('$username','$password','$password2','$firstname','$lastname','$company','$gender','$day','$month','$year','$email','$address','$country',$postal_code,$tel,$fax,$mobile,'$favourite_color');
SQL;
include "connectdb.php";
include "function.php";
$result=mysql_query($sql);
$msg = "";
$resetForm = "";
if($link==0){
$msg = "<p class='red2' align='center'>";
$msg .= "Sorry, you are not able to be registered.";
echo "<meta http-equiv='refresh' content='5; url=re_cus.php' />";
}
else{
echo "<p class='red2' align='center'>Thank you. You are already registered.</p>";
echo "<meta http-equiv='refresh' content='5; url=service.php' />";
$resetForm = "document.getElementById('myForm').reset();";
}
}
mysql_close($link);
?>
อยากให้พอกดปุ่มแล้วมันแสดงหลังปุ่มว่าใช้ได้หรือไม่อะคะ
แต่ถ้าเขียนแยกไฟล์ดีกว่าช่วยแนะนำทีนะคะ
Tag : - - - -