|
|
|
งง มากๆ ครับ ช่วยดูหน่อยครับ เรื่องของ ajax ครับ ผมมี file index_phone.php |
|
|
|
|
|
|
|
ผมมี file index_phone.php
Code (PHP)
<?php session_start();?>
<!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=tis-620" />
<title>Untitled Document</title>
<link rel="stylesheet" href="../style/style1.css" />
<script language="javascript" src="../j_script/ajax_fw.js"></script>
<script>
function add_num(dis,frm){
var url="add_num.php";
var data1=getFormData(frm);
ajaxLoad('post',url,data1,dis);
document.getElementById(dis).innerHTML = "<center><img src=\"images/ind.gif\"/></center>";
//alert('hello');
}
function del_num(del_id){
//alert(del_id);
var url="del_num.php";
var data_del="del_id=" + del_id;
ajaxLoad('post',url,data_del,'');
//document.getElementById(del_id).innerHTML = "<center><img src=\"images/ind.gif\"/></center>";
//document.getElementById(del_id).style.display='none';
}
</script>
</head>
<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<?php
include"../con_db/con_script.php";
$s_id=$_GET['s_id'];
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="110" bgcolor="#000000">
<table width="900" height="110" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="70" background="images/h1.gif">
<table width="900" height="70" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center">
<table width="900" height="35" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="113" align="left" ><strong><font color="#FFFFFF" size="2">ข้อมูลร้านค้า</font></strong></td>
<td width="238" align="left"><strong><font color="#FFFFFF" size="2">ติดต่อ:<?php ?></font></strong></td>
<td width="549" align="right">
<?php
if(isset($_SESSION['user'])){
?>
<a href="logout.php">
<button name="logout">ออกจากระบบ</button></a>
<?php
}else{
if($s_id=="no"){
echo"<strong><font color=\"red\" size=\"2\">ข้อมูลการเข้าระบบไม่ถูกต้อง</font></strong> ";
}
?>
<form method="post" action="ck_login.php"><strong><font color="#FFFFFF" size="2">Username:<input name="u_name" type="text" size="15" /> Password :<input name="u_pass" type="password" size="15" /></font></strong> <input name="submit" type="submit" value="เข้าระบบ" />
</div>
</form>
<?php
}?>
</td>
</tr>
</table>
</td>
</tr>
</table></td>
</tr>
<tr>
<td height="581" bgcolor="#b8efff" valign="top">
<table width="900" height="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="500" align="center" valign="top" bgcolor="#FFFFFF">
<table width="900" height="581" border="0" cellpadding="5" cellspacing="5">
<tr>
<td height="203" valign="top">
<?php
// ส่วนของ เบอร์โทรศัพท์
$u_id=$_SESSION['user'];
$sql_num="select * from shop where shop_id='$u_id' and shop_type='phone' ";
$qry_num=mysql_db_query($db_name,$sql_num);
$row1=mysql_num_rows($qry_num);
if($row1>0){
?>
<table width="800" height="236" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="39" colspan="5" align="left"><strong>เบอร์สวย</strong>
</td>
</tr>
<tr>
<td height="30" valign="top" width="250">
<?php
if(isset($_SESSION['user'])){
?>
<strong>True Move</strong><br/>
<form id="numt">
<strong>เพิ่มเบอร์ :</strong>
<input type="text" name="n_tel" size="15" maxlength="11" /><input type="hidden" name="sys" value="T"/><input type="hidden" name="shop_id" value="<?php echo $_SESSION['user'];?>"/> <button onclick="add_num('true1','numt')">เพิ่ม</button>
</form>
<?php
}
?>
</td>
<td valign="top" width="25"> </td>
<td valign="top" width="250">
<?php
if(isset($_SESSION['user'])){
?>
<strong>DTAC </strong><br/>
<form id="numd">
<strong>เพิ่มเบอร์ :</strong>
<input type="text" name="n_tel" size="15" maxlength="11" /><input type="hidden" name="sys" value="D"/><input type="hidden" name="shop_id" value="<?php echo $_SESSION['user'];?>"/> <button onclick="add_num('dtac','numd')">เพิ่ม</button>
</form>
<?php
}
?>
</td>
<td valign="top" width="25"> </td>
<td valign="top" width="250" >
<?php
if(isset($_SESSION['user'])){
?>
<strong>12CALL</strong><br/>
<form id="numc">
<strong>เพิ่มเบอร์ :</strong>
<input type="text" name="n_tel" size="15" maxlength="11" /><input type="hidden" name="sys" value="C"/><input type="hidden" name="shop_id" value="<?php echo $_SESSION['user'];?>"/> <button onclick="add_num('12call','numc')">เพิ่ม</button>
</form>
<?php
}
?>
</td>
</tr>
<td height="101" valign="top" width="250" id="true1">
<?php
$shop_id=$_SESSION['user'];
$sql_num="select * from numtel where num_com='T' and shop_id=$shop_id";
$res_all="<table width=\"240\" cellspacecing=\"2\" cellpadding=\"2\" align=\"center\" border=\"0\"><tr>";
$re_num=mysql_db_query($db_name,$sql_num);
$count=0;
while($res=mysql_fetch_array($re_num)){
$count +=1;
if($count % 2==0){
$res_all="".$res_all."<td align=\"left\" height=\"20\" class=\"td1\" id=\" ".$res['num_id']."\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >ลบ</a></td>";
$res_all=$res_all."</tr><tr>";
}else{
$res_all="".$res_all."<td align=\"left\" height=\"20\" class=\"td1\" id=\" ".$res['num_id']."\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >ลบ</a></td>";
}
}
$res_all="".$res_all."</tr></table>";
//header("content-type:text/javascript; charset=tis-620");
echo $res_all;
?> </td>
<td valign="top" width="25"> </td>
<td valign="top" width="250" id="dtac">
<?php
$shop_id=$_SESSION['user'];
$sql_num="select * from numtel where num_com='D' and shop_id=$shop_id";
$res_all="<table width=\"240\" cellspacecing=\"2\" cellpadding=\"2\" align=\"center\" border=\"0\"><tr>";
$re_num=mysql_db_query($db_name,$sql_num);
$count=0;
while($res=mysql_fetch_array($re_num)){
$count +=1;
if($count % 2==0){
$res_all="".$res_all."<td align=\"left\" height=\"20\" class=\"td1\" id=\" ".$res['num_id']."\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >ลบ</a></td>";
$res_all=$res_all."</tr><tr>";
}else{
$res_all="".$res_all."<td align=\"left\" height=\"20\" class=\"td1\" id=\" ".$res['num_id']."\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >ลบ</a></td>";
}
}
$res_all="".$res_all."</tr></table>";
//header("content-type:text/javascript; charset=tis-620");
echo $res_all;
?>
</td>
<td valign="top" width="25"> </td>
<td valign="top" width="250" id="12call">
<?php
$shop_id=$_SESSION['user'];
$sql_num="select * from numtel where num_com='C' and shop_id=$shop_id";
$res_all="<table width=\"240\" cellspacecing=\"2\" cellpadding=\"2\" align=\"center\" border=\"0\"><tr>";
$re_num=mysql_db_query($db_name,$sql_num);
$count=0;
while($res=mysql_fetch_array($re_num)){
$count +=1;
if($count % 2==0){
$res_all="".$res_all."<td align=\"left\" height=\"20\" class=\"td1\" id=\" ".$res['num_id']."\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >ลบ</a></td>";
$res_all=$res_all."</tr><tr>";
}else{
$res_all="".$res_all."<td align=\"left\" height=\"20\" class=\"td1\" id=\" ".$res['num_id']."\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >ลบ</a></td>";
}
}
$res_all="".$res_all."</tr></table>";
//header("content-type:text/javascript; charset=tis-620");
echo $res_all;
?>
</td>
</tr>
</table>
<?php
}
?>
</td>
</tr>
<tr>
<td height="176"> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="80" background="images/fit.gif"> </td>
</tr>
</table>
</body>
</html>
Tag : - - - -
|
|
|
|
|
|
Date :
2010-04-17 18:16:36 |
By :
edword |
View :
890 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file ที่ 2 ชื่อ add_num.php
Code (PHP)
<?php
header("content-type:text/plain; charset=tis-620");
include"../con_db/con_script.php";
$n_tel=$_POST['n_tel'];
$sys=$_POST['sys'];
$shop=$_POST['shop_id'];
if($n_tel==""){
show_num();
}else{
$sql_add="insert into numtel values('','$n_tel','$sys',$shop)";
mysql_db_query($db_name,$sql_add);
show_num();
}
function show_num(){
$sql_num="select * from numtel where num_com='$sys' and shop_id=$shop";
$res_all="<table width=\"240\" cellspacecing=\"2\" cellpadding=\"2\" align=\"center\" border=\"0\"><tr>";
$re_num=mysql_db_query($db_name,$sql_num);
$count=0;
while($res = mysql_fetch_array($re_num)){
$count +=1;
if($count % 2 ==0){
$res_all="".$res_all."<td height=\"20\" class=\"td1\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >del</a></td>";
$res_all=$res_all."</tr><tr>";
}else{
$res_all="".$res_all."<td height=\"20\" class=\"td1\">".$res['num_ber']." <a href=\"javascript:del_num(".$res['num_id'].")\" >del</a></td>";
}
}
$res_all="".$res_all."</tr></table>";
echo $res_all;
}
?>
|
|
|
|
|
Date :
2010-04-17 18:18:59 |
By :
edword |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พอทำการเพิ่ม ข้อมูลที่ file index_phone.php
ใน form ที่บรรทัดที่ 108 เพื่อส่ง แบบ ajax ไปที่ file add_num.php
แล้วนำมาแสดงผลที่ บรรทัดที่ 143 (id="true1")
แต่สิ่งแสดงกลับมา ที่ บรรทัดที่ 143 (id="true1") บอกว่า
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\AppServ\www\CM-NANA\open_shop\add_num.php on line 19
ทั้งๆที่ข้อมูลก็ถูกเก็บลง database
รบกวนขอถามผู้รู้หน่อยครับ หรือว่า ajax ไม่สามารถส่งข้อมูลที่มีการ query จาก database ได้ยครับ
ขอบคุณมากครับ
อ้อ code ajax มีดังนี้ครับ
Code (PHP)
function ajaxLoad(method, URL, data, displayId) {
var ajax = null;
if(window.ActiveXObject) {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
}
else {
alert("Your browser doesn't support Ajax");
return;
}
method = method.toLowerCase();
URL += "?dummy=" + (new Date()).getTime();
if(method=="get") {
URL += "&" + data;
data = null;
}
ajax.open(method, URL);
if(method=="post") {
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
ajax.onreadystatechange = function() {
if(ajax.readyState==4 && ajax.status==200) {
var ctype = ajax.getResponseHeader("Content-Type").toLowerCase();
ajaxCallback(ctype, displayId, ajax.responseText);
delete ajax;
ajax = null;
}
}
ajax.send(data);
}
function ajaxCallback(contentType, displayId, responseText) {
if(contentType.match("text/javascript")) {
eval(responseText);
}
else {
var el = document.getElementById(displayId);
el.innerHTML = responseText;
}
}
function getFormData(form_name_or_id) {
var frm = document.forms[form_name_or_id];
if(frm==null) {
alert("form not found!");
return;
}
var data = "";
var num_el = frm.elements.length;
for(i=0; i<num_el; i++) {
var el = frm.elements[i];
if(el.name=="" && el.id=="") {
continue;
}
var param_name = "";
if(el.name!="") {
param_name = el.name;
}
else if(el.id!="") {
param_name = el.id;
}
var t = frm.elements[i].type;
var value = "";
if(t=="text"||t=="password"||t=="hidden"||t=="textarea") {
value = encodeURI(el.value);
}
else if(t=="radio"||t=="checkbox") {
if(el.checked) {
value = encodeURI(el.value);
}
else {
continue;
}
}
else if(t=="select-one") {
value = encodeURI(el.options[el.selectedIndex].value);
}
else if(t=="select-multiple") {
for(j=0; j<el.length; j++) {
if(el.options[j].selected) {
if(data!="") {
data += "&";
}
data += param_name + "=";
data += encodeURI(select.options[j].value);
}
}
continue;
}
if(data!="") {
data += "&";
}
data += param_name + "=" + value;
}
return data;
}
|
|
|
|
|
Date :
2010-04-17 18:28:35 |
By :
edword |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คิดว่า ตามความเห็นที่ 3 คิดว่า ต้องรับค่า $sys และ $shop ใน function นะครับ
|
|
|
|
|
Date :
2010-04-22 16:29:11 |
By :
nilas |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|