|
|
|
กดปุ่ม cancel หรือ delete แล้วข้อมูลที่แสดงมันจะหายไปไม่แสดง |
|
|
|
|
|
|
|
AjaxPHPEditRecord1.php
Code (PHP)
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="JavaScript">
var HttpRequest = false;
<!-- // ********************************* -->
function checkBrowser(){
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;
}
}
<!-- //************************************** -->
function doCallAjax(Mode) {
HttpRequest = false;
checkBrowser();
var url = 'AjaxPHPEditRecord2.php';
if(Mode == "ADD") {
var pmeters = "tCustomerID=" + encodeURI( document.getElementById("atxtCustomerID").value) +
"&tName=" + encodeURI( document.getElementById("atxtName").value ) +
"&tEmail=" + encodeURI( document.getElementById("atxtEmail").value ) +
"&tCountryCode=" + encodeURI( document.getElementById("atxtCountryCode").value ) +
"&tBudget=" + encodeURI( document.getElementById("atxtBudget").value ) +
"&tUsed=" + encodeURI( document.getElementById("atxtUsed").value ) +
"&tMode=" + Mode;
alert(pmeters);
}
if(Mode == "UPDATE") {
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;
}
if(Mode == "LIST") {
var pmeters = "";
}
HttpRequest.open('POST',url,true);
HttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
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 = "<image src='images/ajax_load.gif'>";
}
if(HttpRequest.readyState == 4) // Return Request
{
document.getElementById("editForm").style.display = 'none';
document.getElementById("addForm").style.display = 'none';
document.getElementById("startAdd").style.display = '';
document.getElementById("txtCustomerID").value = '';
document.getElementById("txtName").value = '';
document.getElementById("txtEmail").value = '';
document.getElementById("txtCountryCode").value = '';
document.getElementById("txtBudget").value = '';
document.getElementById("txtUsed").value = '';
document.getElementById("mySpan").innerHTML = HttpRequest.responseText;
}
}
}
function ShowADD() {
document.getElementById("addForm").style.display = '';
document.getElementById("editForm").style.display = 'none';
document.getElementById("startAdd").style.display = 'none';
document.getElementById("atxtCustomerID").value ='';
document.getElementById("atxtName").value = '';
document.getElementById("atxtEmail").value = '';
document.getElementById("atxtCountryCode").value ='';
document.getElementById("atxtBudget").value ='';
document.getElementById("atxtUsed").value = '';
}
function ShowEdit(sCustomerID,sName,sEmail,sCountryCode,sBudget,sUsed) {
document.getElementById("editForm").style.display = '';
document.getElementById("addForm").style.display = 'none';
document.getElementById("startAdd").style.display = 'none';
document.getElementById("txtCustomerID").value = sCustomerID;
document.getElementById("txtName").value = sName;
document.getElementById("txtEmail").value = sEmail;
document.getElementById("txtCountryCode").value = sCountryCode;
document.getElementById("txtBudget").value = sBudget;
document.getElementById("txtUsed").value = sUsed;
}
function ajaxDelete(Mode, txtCustomerID) {
HttpRequest = false;
checkBrowser();
//alert('test');
var url = 'AjaxPHPEditRecord2.php';
var pmeters = "tCustomerID=" + txtCustomerID + "&tMode=" + Mode;
HttpRequest.open('POST',url,true);
HttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
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;
doCallAjax('LIST');
}
}
}
</script>
<body Onload="JavaScript:doCallAjax('LIST');">
<h1>My Customer</h1>
<form name="frmMain">
<span id="editForm" style="display='none';">
<table width="700" border="1">
<tr>
<th width="91">CustomerID</th>
<th width="98">Name</th>
<th width="198">Email</th>
<th width="97">CountryCode</th>
<th width="59">Budget</th>
<th width="71">Used</th>
</tr>
<tr>
<td><input type="text" name="txtCustomerID" id="txtCustomerID" size="5"></td>
<td><input type="text" name="txtName" id="txtName" size="25"></td>
<td><input type="text" name="txtEmail" id="txtEmail" size="25"></td>
<td><input type="text" name="txtCountryCode" id="txtCountryCode" size="2"></td>
<td align="right"><input type="text" name="txtBudget" id="txtBudget" size="5"></td>
<td align="right"><input type="text" name="txtUsed" id="txtUsed" size="5"></td>
</tr>
</table>
<input type="button" name="btnUpdate" id="btnUpdate" value="Update" OnClick="JavaScript:doCallAjax('UPDATE');">
<input type="button" name="btnCancelUpdate" id="btnCancelUpdate" value="CancelUpdate" OnClick="JavaScript:doCallAjax('LIST');">
<br><br>
</span>
<span id="addForm" style="display='none';">
<table width="700" border="1">
<tr>
<th width="91">CustomerID</th>
<th width="98">Name</th>
<th width="198">Email</th>
<th width="97">CountryCode</th>
<th width="59">Budget</th>
<th width="71">Used</th>
</tr>
<tr>
<td><input name="atxtCustomerID" type="text" id="atxtCustomerID" size="5"></td>
<td><input type="text" name="atxtName" id="atxtName" size="25"></td>
<td><input type="text" name="atxtEmail" id="atxtEmail" size="25"></td>
<td><input type="text" name="atxtCountryCode" id="atxtCountryCode" size="2"></td>
<td align="right"><input type="text" name="atxtBudget" id="atxtBudget" size="5"></td>
<td align="right"><input type="text" name="atxtUsed" id="atxtUsed" size="5"></td>
</tr>
</table>
<input type="button" name="btnADD" id="btnADD" value="ADD" OnClick="JavaScript:doCallAjax('ADD');">
<input type="button" name="btnCancelADD" id="btnCancelADD" value="CancelADD" OnClick="JavaScript:doCallAjax('LIST');">
<br><br>
</span>
<div id="startAdd" style="display='none';">
<input type="button" name="btnShowADD" id="btnShowADD" value="ADD" OnClick="JavaScript:ShowADD();">
</div>
<span id="mySpan"></span>
</form>
</body>
</html>
AjaxPHPEditRecord1.php
Code (PHP)
<?
session_start();
$ss= $_SESSION['name']=$_POST['tName'];
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
include "../config.inc.php";
mysql_select_db('customer');
$strMode = $_POST["tMode"];
$strID = $_POST["tCustomerID"];
if($strMode == "ADD") {
$strSQL = "INSERT INTO customer SET ";
$strSQL .="CustomerID = '".$_POST["tCustomerID"]."' ";
$strSQL .=",Name = '".$_POST["tName"]."' ";
$strSQL .=",Email = '".$_POST["tEmail"]."' ";
$strSQL .=",CountryCode = '".$_POST["tCountryCode"]."' ";
$strSQL .=",Budget = '".$_POST["tBudget"]."' ";
$strSQL .=",Used = '".$_POST["tUsed"]."' ";
// $strSQL .="WHERE CustomerID = '".$_POST["tCustomerID"]."' ";
$objQuery = mysql_query($strSQL);
} else if($strMode == "UPDATE") {
$strSQL = "UPDATE customer SET ";
$strSQL .="Name = '".$_POST["tName"]."' ";
$strSQL .=",Email = '".$_POST["tEmail"]."' ";
$strSQL .=",CountryCode = '".$_POST["tCountryCode"]."' ";
$strSQL .=",Budget = '".$_POST["tBudget"]."' ";
$strSQL .=",Used = '".$_POST["tUsed"]."' ";
$strSQL .="WHERE CustomerID = '".$_POST["tCustomerID"]."' ";
$objQuery = mysql_query($strSQL);
} else if($strMode == "DELETE") {
$strSQL = "DELETE FROM customer ";
$strSQL .="WHERE CustomerID = '".$strID."' ";
$objQuery = mysql_query($strSQL);
}
echo "$ss";
$strSQL = "SELECT * FROM customer WHERE Name ='$ss' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="700" border="1">
<tr>
<th width="91"> <div align="center">CustomerID</div></th>
<th width="158"> <div align="center">Name</div></th>
<th width="198"> <div align="center">Email</div></th>
<th width="97"> <div align="center">CountryCode</div></th>
<th width="59"> <div align="center">Budget</div></th>
<th width="71"> <div align="center">Used</div></th>
<th width="40"> <div align="center">Edit</div></th>
<th width="40"> <div align="center">Delete</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>
<td align="center"><a href="JavaScript:ShowEdit('<?=$objResult["CustomerID"];?>','<?=$objResult["Name"];?>','<?=$objResult["Email"];?>','<?=$objResult["CountryCode"];?>','<?=$objResult["Budget"];?>','<?=$objResult["Used"];?>')">Edit</a></td>
<td align="center"><a href="JavaScript:ajaxDelete('DELETE','<?=$objResult["CustomerID"];?>');" onClick="return confirm('Are you sure you want to delete?')">Del</a></td>
</tr>
<?
}
?>
</table>
</body>
</html>
///////////////////////////////////////////
Code (PHP)
$strSQL = "SELECT * FROM customer WHERE Name ='$ss' ";
ถ้าไม่ใส่ where มันจะแสดงทั้งหมด
Tag : PHP, Ajax
|
ประวัติการแก้ไข 2012-09-26 17:02:16
|
|
|
|
|
Date :
2012-09-25 13:27:56 |
By :
praphat_thekop |
View :
1064 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้า ใส่แค่นี้ดูได้ไม่หาย
Code (PHP)
$strSQL = "SELECT * FROM customer ";
|
|
|
|
|
Date :
2012-09-25 15:44:55 |
By :
praphat_thekop |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูใน php ครับ มันส่งค่าไปหรือเปล่าครับ
|
|
|
|
|
Date :
2012-09-28 09:34:59 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|