|
 |
|
สอบถามปัญหา ajax หน่อยครับ งงโค้ดไม่ทำงานตามเงื่อนไขที่กำหนด ดูรายละเอียดข้างในเลยครับ |
|
 |
|
|
 |
 |
|
AJAX
function showdatasql()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("showdata").innerHTML=xmlhttp.responseText;
}
}
var str=Math.random();
var querystr="";
querystr+="show_data.php?rd="+str;
//alert(querystr);
xmlhttp.open("GET",querystr,true);
xmlhttp.send(null);
}
////////////////////////////////////////////////
function showaddsql()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//alert(xmlhttp.responseText); ตรงนี้ใช้ได้ แสดงตามเงื่อนไขเลย
แต่ถ้ามาใช้ if เพื่อให้แสดงให้ id ที span กลับไม่ทำงาน ******
if(xmlhttp.responseText=='a1'){
document.getElementById("noname").innerHTML="<font color=red>**</font> ยังไม่ได้ใส่ชื่อ";
}else if(xmlhttp.responseText=='a2'){
document.getElementById("notel").innerHTML="<font color=red>**</font> ยังไม่ได้เบอร์โทรศัพท์";
}else if(xmlhttp.responseText=='a3'){
document.getElementById("noemail").innerHTML="<font color=red>**</font> ยังไม่ได้เบอร์อีกเมล์";
}else if(xmlhttp.responseText=='a4'){
document.getElementById("noname").innerHTML="<font color=red>**</font ชื่อนี้ถูกใช้แล้ว";
}else{
document.getElementById("ook").innerHTML="เพิ่มข้อมูลเรียบร้อยแล้ว";
}
***********************************************************************
}
else
{
document.getElementById("showdata2").innerHTML="loading...";
}
}
var str=Math.random();
var name=document.myform.name.value;
var tel=document.myform.tel.value;
var email=document.myform.email.value;
var querystr="add.php"
querystr+="?rd="+str;
querystr+="&name="+name;
querystr+="&tel="+tel;
querystr+="&email="+email;
//alert(querystr2);
xmlhttp.open("GET",querystr,true);
xmlhttp.send(null);
}
form ส่ง
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>index web ajex</title>
<link href="../style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="function/function_ajax.js"></script>
</head>
<body onload="showdatasql();">
<div id="all">
<table id="tall" border="1">
<tr id="header">
<th colspan="2"><p> </p>
<p class="style1">NamE site </p>
<p> </p></th>
</tr>
<tr id="menu">
<th><a href="#" >แสดงข้อมูล</a></th>
<th>test</th>
</tr>
<tr>
<td colspan="2" id="showdata"> </td>
</tr>
<tr>
<td colspan="2" id="showdata2 ">
<form name="myform" method="post" action="#">
************************************
<table width="55%" border="0" align="center" cellspacing="0" id="adddata">
<tr>
<td width="23%" align="right">ชื่อ :</td>
<td width="77%"><input type="text" name="name" /><span id="noname"></span></td>
</tr>
<tr>
<td align="right">เบอร์โทร : </td>
<td><input type="text" name="tel" /><span id="notel"></span></td>
</tr>
<tr>
<td align="right">อีเมล์ : </td>
<td><input type="text" name="email" /><span id="noemail"></span></td>
</tr>
<tr>
<td align="right"><span id="ook"></span></td>
<td><input type="submit" name="button" value="เพิ่มข้อมูล" onclick="showaddsql();" > </td>
</tr>
</table>
************************************
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
add.php
<?
include "config.php";
if($_GET['name']==""){
echo "a1";
exit();
}
if($_GET['tel']==""){
echo "a2";
exit();
}
if($_GET['email']==""){
echo "a3";
exit();
}
$sql = "SELECT * FROM tb_data WHERE name ='$_GET[name]' ";
$query = mysql_query($sql) or die ("Error Query".mysql_error());
$result = mysql_fetch_array($query);
if($result)
{
echo "a4";
}
else
{
$sql="INSERT INTO tb_data (name,tel,email)values('$_GET[name]','$_GET[tel]','$_GET[email]')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "ok";
}
?>
Tag : PHP, MySQL

|
ประวัติการแก้ไข 2011-05-23 10:56:15
|
 |
 |
 |
 |
Date :
2011-05-23 10:54:33 |
By :
ckcr |
View :
847 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันทำตามเงื่อนไข แต่ ไม่ แสดงผล ใน span ของแต่ละ id ครับ
รูปแบบไรผิดเปล่าครับ ถึงไป แสดงผลใน id ที่กำหนด ครับ
|
 |
 |
 |
 |
Date :
2011-05-23 11:04:03 |
By :
ckcr |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|