|
|
|
ajax เพิ่ม,แก้ไข,ลบ มีปัญหาครับ แก้ไขกับลบ ผ่านแล้ว แต่ติดที่เพิ่มข้อมูลไม่ได้สักที เรียนผู้รู้ช่วยทีครับ |
|
|
|
|
|
|
|
code accountList.php
Code (PHP)
<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 = 'formInsert/setting/accountListSave.php';
if(Mode == "ADD") {
// var pmeters = "tCustomerID=" + encodeURI( document.getElementById("txtCustomerID").value) +
var pmeters ="tListID=" + encodeURI( document.getElementById("alistID").value ) +
"tListName=" + encodeURI( document.getElementById("alistName").value ) +
"&tListDetail=" + encodeURI( document.getElementById("alistDetail").value ) +
"&tMode=" + Mode;
alert(pmeters);
}
if(Mode == "UPDATE") {
var pmeters = "tListID=" + encodeURI( document.getElementById("listID").value) +
"&tListName=" + encodeURI( document.getElementById("listName").value ) +
"&tListDetail=" + encodeURI( document.getElementById("listDetail").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("listID").value = '';
document.getElementById("listName").value = '';
document.getElementById("listDetail").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("alistID").value ='';
document.getElementById("alistName").value = '';
document.getElementById("alistDetail").value = '';
}
function ShowEdit(slistID,slistName,slistDetail) {
document.getElementById("editForm").style.display = '';
document.getElementById("addForm").style.display = 'none';
document.getElementById("startAdd").style.display = 'none';
document.getElementById("listID").value = slistID;
document.getElementById("listName").value = slistName;
document.getElementById("listDetail").value = slistDetail;
}
function ajaxDelete(Mode, listID) {
HttpRequest = false;
checkBrowser();
//alert('test');
var url = 'formInsert/setting/accountListSave.php';
var pmeters = "tListID=" + listID + "&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');">
<form name="frmMain">
<span id="editForm" style="display='none';">
<table width="700" border="1">
<tr>
<th> <div align="center">รหัสรายการ</div></th>
<th> <div align="center">ชื่อรายการ</div></th>
<th> <div align="center">รายละเอียด</div></th>
</tr>
<tr>
<td><div align="center"><input type="text" class="form-control" name="listID" id="listID" size="5" disabled="true"></div></td>
<td><input type="text" class="form-control" name="listName" id="listName" size="20"></td>
<td><input type="text" class="form-control" name="listDetail" id="listDetail" size="20"></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> <div align="center">รหัสรายการ</div></th>
<th> <div align="center">ชื่อรายการ</div></th>
<th> <div align="center">รายละเอียด</div></th>
</tr>
<tr>
<td><div align="center"><input type="text" class="form-control" name="listID" id="listID" size="5" ></div></td>
<td><input type="text" class="form-control" name="listName" id="listName" size="20"></td>
<td><input type="text" class="form-control" name="listDetail" id="listDetail" size="20"></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>
code accountListSave.php
Code (PHP)
<?php
include ("../../config/config_DB.php");
$strMode = $_POST["tMode"];
$strID = $_POST["tlistID"];
if($strMode == "ADD") {
$strSQL = "INSERT INTO tb_account_list ";
$strSQL .="(List_ID,List_Name,List_Detail) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["tListID"]."','".$_POST["tListName"]."','".$_POST["tListDetail"]."') ";
$objQuery = mysql_query($strSQL);
} else if($strMode == "UPDATE") {
$strSQL = "UPDATE tb_account_list SET ";
$strSQL .="List_Name = '".$_POST["tListName"]."' ";
$strSQL .=",List_Detail = '".$_POST["tListDetail"]."' ";
$strSQL .="WHERE List_ID = '".$_POST["tListID"]."' ";
$objQuery = mysql_query($strSQL);
} else if($strMode == "DELETE") {
$strSQL = "DELETE FROM tb_account_list ";
$strSQL .="WHERE List_ID = '".$_POST["tListID"]."' ";
$objQuery = mysql_query($strSQL);
}
$strSQL = "SELECT * FROM tb_account_list ORDER BY List_ID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table border="1">
<tr>
<th><div align="center">รหัสรายการ</div></th>
<th><div align="center">ชื่อรายการ</div></th>
<th><div align="center">รายละเอียด</div></th>
<th><div align="center">Edit</div></th>
<th><div align="center">Delete</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["List_ID"];?></div></td>
<td><?=$objResult["List_Name"];?></td>
<td><?=$objResult["List_Detail"];?></td>
<td align="center"><a href="JavaScript:ShowEdit('<?=$objResult["List_ID"];?>','<?=$objResult["List_Name"];?>','<?=$objResult["List_Detail"];?>')">Edit</a></td>
<td align="center"><a href="JavaScript:ajaxDelete('DELETE','<?=$objResult["List_ID"];?>');" onclick="return confirm('Are you sure you want to delete?')">Del</a></td>
</tr>
<?
}
?>
</table>
<?
mysql_close();
?>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2014-06-25 00:34:38 |
By :
san.saleah |
View :
786 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าคุณยังไม่มั่นใจเรื่อง php อย่าเพิ่งเอา ajax มาเกี่ยว ลองรันแบบธรรมดาดูก่อนะครับ
mysql_query($strSQL) or die(mysql_error());
mysql_error() เป็นฟังชันก์พื้นฐานสำหรับเทส mysql เลยครับ เวลามีข้อผิดพลาดอะไร เจ้าสิ่งนี้จะแสดงข้อความที่ผิดพลาด จะช่วยเราได้เยอะกว่าครับ
|
|
|
|
|
Date :
2014-06-25 00:53:39 |
By :
xbeginner01 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือผมกด ADD แต่หน้าเพจ ไม่เกิดอะไรขึ้นเลยนะครับ ลองใช้ mysql_query($strSQL) or die(mysql_error()); ก็ไม่เกิดอะไรเหมือนกันครับพี่
|
|
|
|
|
Date :
2014-06-25 00:59:39 |
By :
san.saleah |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ปิดกระทู้ ครับ แก้ไขแล้ว ขอบคุณมากครับ
|
|
|
|
|
Date :
2014-06-25 01:28:29 |
By :
san.saleah |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดไปครับ
|
|
|
|
|
Date :
2014-06-25 08:18:14 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|