|
 |
|
ถามเรื่อง การส่งตัวแปร เข้าไปให้ Ajax น่ะครับ ทำยังไงถึงจะส่งตัวแปรเข้าไปให้ Ajax ได้?? |
|
 |
|
|
 |
 |
|
ดูโค๊ดก่อนนะครับ
โค๊ดเอามาจากเว็บนี้แหละคับ
Code (PHP)
<?php
/*** By Weerachai Nukitram***/
/*** http://www.ThaiCreate.Com ***/
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(Sort) {
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 = 'AjaxPHPListRecord2.php';
var pmeters = 'mySort='+Sort;
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
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
</script>
<body Onload="JavaScript:doCallAjax('CustomerID');">
<h1>My Customer</h1>
<span id="mySpan"></span>
</body>
</html>
อันนี้คือไฟล์ AjaxPHPListRecord1.php นะครับ คือผมต้องการจะส่ง ตัวแปร อะไรก็ได้เข้าไปใน Ajax ตัวนี้
เพื่อให้ไฟล์ AjaxPHPListRecord2.php มารับเอาน่ะครับ
ดูไฟล์ AjaxPHPListRecord2.php นะครับ
Code (PHP)
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
$strSort = $_POST["mySort"];
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer ORDER BY $strSort ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center"><a href="JavaScript:doCallAjax('CustomerID')">CustomerID</a></div></th>
<th width="98"> <div align="center"><a href="JavaScript:doCallAjax('Name')">Name</a> </div></th>
<th width="198"> <div align="center"><a href="JavaScript:doCallAjax('Email')">Email</a> </div></th>
<th width="97"> <div align="center"><a href="JavaScript:doCallAjax('CountryCode')">CountryCode</a> </div></th>
<th width="59"> <div align="center"><a href="JavaScript:doCallAjax('Budget')">Budget</a> </div></th>
<th width="71"> <div align="center"><a href="JavaScript:doCallAjax('Used')">Used</a> </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
จุดประสงค์ของผมคือ ผมจะส่งตัวแปร ที่เก็บ ชื่อตารางไว้น่ะครับ
คือ เลือกตาราง ที่จะแสดงผลได้
เช่น Code (PHP)
$strSQL = "SELECT * FROM ตัวแปร ที่เก็บ ชื่อตาราง ORDER BY $strSort ASC ";
ประมาณนี้คับ
ถ้าไม่เข้าใจคำถาม บอกนะคับ ผมต้องการความช่วยเหลือจริงๆคับ ^^
Tag : PHP, Ajax

|
|
 |
 |
 |
 |
Date :
2011-05-09 16:54:47 |
By :
akecsubu13 |
View :
833 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คำถาม ตามหัวข้อน่ะครับ ทำยังไงถึงจะส่งตัวแปร เข้าไปใน Ajax ได้
|
 |
 |
 |
 |
Date :
2011-05-09 16:56:03 |
By :
akecsubu13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (JavaScript)
var url = 'AjaxPHPListRecord2.php';
var pmeters = '?mySort='+Sort;
var name = '&name=customerName';
HttPRequest.open('POST',url+pmeters+name,true);
อยากจะส่งอะไรก็เพิ่มหลัง url
|
 |
 |
 |
 |
Date :
2011-05-10 02:58:37 |
By :
adamdemister |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
var url = 'AjaxPHPInsertRecord2.php';
var pmeters = "tCustomerID=" + encodeURI( document.getElementById("txtCustomerID").value) +
"&tName=" + encodeURI( document.getElementById("txtName").value ) +
"&tEmail=" + encodeURI( document.getElementById("txtEmail").value ) +
"&tCountryCode=" + encodeURI( document.getElementById("txtCountryCode").value ) +
"&tBudget=" + encodeURI( document.getElementById("txtBudget").value ) +
"&tUsed=" + encodeURI( document.getElementById("txtUsed").value ) +
"&tMode=" + Mode;
ตัวอย่างการส่งครับ
|
 |
 |
 |
 |
Date :
2011-05-10 06:45:50 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|