|
 |
|
ajax ไม่ทำงาน เป้นเพราะอะไรครับช่วยดูหน่อยครับ หัดเขียนและลองความทำเข้าใจดูแล้วแต่ ก็นิ่งเลย |
|
 |
|
|
 |
 |
|
form ajax
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>aaaaaaaaaaaaaa</title>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 't-1_add_action.php';
var pmeters = "tname=" + encodeURI( document.getElementById("name").value) +
"&ttel=" + encodeURI( document.getElementById("tel").value ) +
"&temail=" + encodeURI( document.getElementById("email").value );
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
if (HttPRequest.responseText == 'a1') {
document.getElementById("a1").innerHTML ="กรอกชื่อ";
}
else if (HttPRequest.responseText == 'a2') {
document.getElementById("a2").innerHTML = "กรอกเบอร์";
}
else if (HttPRequest.responseText == 'a3') {
document.getElementById("a3").innerHTML = "กรอกอีเมล์";
}
else if(HttPRequest.responseText == 'Y') {
document.getElementById("mySpan").innerHTML = "เพิ่มเรียบร้อย";
}
else {
document.getElementById("a1").innerHTML = "ใช้แล้ว";
}
}
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<table width="388" border="0" align="center">
<tr>
<th colspan="2">เพิ่มข้อมูล</th>
</tr>
<tr>
<td>ชื่อ</td>
<td><input type="text" name="name" id="name"><span id="a1"></span></td>
</tr>
<tr>
<td>เบอร์โทร</td>
<td><input type="text" name="tel" id="tel"><span id="a2"></span></td>
</tr>
<tr>
<td>อีเมล์</td>
<td><input type="text" name="email" id="eamil"><span id="a2"></span></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" OnClick="JavaScript:doCallAjax();" value="Register"></td>
</tr>
</table>
</form>
<span id="mySpan">
</body>
</htmt>
t-1_add_action.php
<?php
$name = trim($_POST["tname"]);
$tel= trim($_POST["ttel"]);
$email = trim($_POST["temail"]);
//*** Check name***//
if(trim($name) == "")
{
echo "a1";
exit();
}
//*** Check tel***//
if(trim($tel) == "")
{
echo "a2";
exit();
}
//*** Check email***//
if(trim($email) == "")
{
echo "a3";
exit();
}
include "config.php";
//*** Check Username (already exists) ***//
$sql = "SELECT * FROM tb_data WHERE name = '$name' ";
$query = mysql_query($sql) or die ("Error Query [".$strSQL."]");
$result = mysql_fetch_array($query);
if($result)
{
echo "a1";
}
else
{
$sql = "INSERT INTO tb_data (name,tel,email)VALUES('$name','$tel','$email')";
$query = mysql_query($sql);
if (!$query)
{
die('Error: ' . mysql_error());
}
echo "Y";
}
?>
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2011-05-20 16:41:58 |
By :
ckcr |
View :
873 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่ามันทำแต่มันไม่แสดงผลลัพธ์ที่ต้องการน่ะ 
|
 |
 |
 |
 |
Date :
2011-05-20 17:58:15 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แสดง ครับ ลอง ใช้ วิธี php ดูแล้ว ครับ
แสดง ตามเงื่อนไขเลยครับ เพิ่ม ได้ ครับ
พอมาใส่ เป็น AJEX แล้วนิ่งเลยงง ว่า เขียนผอดรูปแบบเปล่า
ตอนคลิก ขึ้น "Now is Loading..." แวๆ
มีวิธีตรวจไมครับ ว่า มัน ทำงาน ไม
หรือว่าส่ง ที่ url ไม ดูแล้วคล้ายทำงาน แต่ ไม่มีไรเกิดขึ้น
|
 |
 |
 |
 |
Date :
2011-05-21 00:26:35 |
By :
ckcr |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เคยแก้ปัญหานี้ให้น้องคนนึง แต่ใช้โค๊ดผมนะ ไว้เดี๋ยวเอามาแปะให้
|
 |
 |
 |
 |
Date :
2011-05-21 14:39:10 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับผม ก็ลองดู หลังจากทำความเข้าใจ กับ JavaScript พอสมควร เลยเล่นดู
มึนเลย!
|
ประวัติการแก้ไข 2011-05-21 15:51:04
 |
 |
 |
 |
Date :
2011-05-21 15:18:10 |
By :
ckcr |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|