|
 |
|
เวลาจะลบ ข้อมูล ต้องการเช็คว่ามีข้อมูลนั้น ใน ตารางอื่นอยู่หรือไม่ ใช้ AJax ไม่รู้จะเช็คตรงไหน ช่วยทีครับ |
|
 |
|
|
 |
 |
|
Code Ajax
Code (JavaScript)
function ajaxDelete(Mode,ID) {
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 = 'AClass_insert.php';
var pmeters = "tMode=" + Mode +
"&tID=" + ID;
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
{
window.location.reload();
}
}
}
นี่หน้า Post ของ Ajax
Code (PHP)
$strMode = $_POST["tMode"];
$strID = $_POST["tID"];
if($strMode == "DELETE") {
$strSQL = "DELETE FROM animal_class ";
$strSQL .="WHERE Class_Code = '".$strID."' ";
$objQuery = mysql_query($strSQL);
}
ปัญหาของผมก็คือ
1. ถ้าผมจะเช็คใน AJax ผมก้อต้องต้อง แทรก php เข้าไปใน Ajax แต่เวลา จะ Select มา มันต้อง WHere ที่ ID นั้น ซึ่ง ผมไม่รุจะเอาค่าของ Javascript มาใช้ใน PHP อย่างไร
2. ถ้าผมจะเช็คในหน้า Post ของ AJax เช็คได้แต่ เวลา ผมจะ ให้มัน Alert เตือน โดยใช้คำสั่ง
Code (PHP)
echo "<script type=\"text/javascript\">window.alert(\"ไม่สามารถลบข้อมูลนี่ได้เนื่องจากมีการใช้งานในตารางอื่น\")</script>";
ซึ่งมันก็ไม่ ยอม Alert ครับ คงเป็นเพราะมัน ยุ หน้า Post ของ Ajax
ผมจะแก้ไขอย่างไร ดีครับ
Tag : PHP, JavaScript, Ajax

|
ประวัติการแก้ไข 2011-12-17 17:36:40
|
 |
 |
 |
 |
Date :
2011-12-17 17:33:11 |
By :
forfunza |
View :
1164 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$strMode = $_POST["tMode"];
$strID = $_POST["tID"];
if($strMode == "DELETE") {
$strSQL = " SELECT * FROM table WHERE 1 AND Class_Code = '".$strID."' ";
$objQuery = mysql_query($strSQL);
$Rows = mysql_num_rows($Rows);
if($Rows > 0 )
{
echo "Cannot Delete";
}else{
$strSQL = "DELETE FROM animal_class ";
$strSQL .="WHERE Class_Code = '".$strID."' ";
$objQuery = mysql_query($strSQL);
}
}
|
 |
 |
 |
 |
Date :
2011-12-17 22:45:10 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ มากครับ พี่ผมติดตรงที่ อยากให้มันเป็น Alert แจ้ง แทน echo "Cannot Delete";
อีกอย่าง ถึงผมใช้ echo "Cannot Delete"; มัน ไม่ลบ ออกจาก Table จิง แต่มัน ไม่ echo ออก มาอะครับ T^T
|
 |
 |
 |
 |
Date :
2011-12-17 23:43:04 |
By :
forfunza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
echo "N";
ฝั่ง Ajax ใช้ JavaScript ตรวจสอบค่า N ครับ
|
 |
 |
 |
 |
Date :
2011-12-18 08:22:07 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ม่ะกี้นึกว่าได้แล้ว T^T
ขอถามอีกทีนะครับ ผมคิดว่า พอเรา
Code (PHP)
echo "N";
ไปแล้ว ในฝั่ง Javascript ก็แค่ สร้างพารามิเตอร์มา รับมัน 1 ตัว แล้วตรวจสอบ ไม่รุ ผมเข้าใจถูกป่ะนะครับ
แต่ทีนี้ผมก้อเพิ่ม ไปที่
Code (JavaScript)
function ajaxDelete(Mode,ID,RP) { / <<<< ลองเพิ่มตรงนี้ ก็เป็นค่าว่าง
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 = 'AClass_insert.php';
var pmeters = "tMode=" + Mode +
"&tID=" + ID;
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
{
if(HttPRequest.responseText=='N'){/ <<<<<< ลองเพิ่ม แบบนี้ ก็ไม่ได้
window.alert("dfas");
}
window.location.reload();
}
}
}
รบกวนอีกทีนะครับ ขอโทษจริงๆ Y&Y
|
ประวัติการแก้ไข 2011-12-18 23:55:33 2011-12-19 01:19:03 2011-12-19 01:21:11 2011-12-19 01:22:28 2011-12-19 01:48:25
 |
 |
 |
 |
Date :
2011-12-18 23:52:10 |
By :
forfunza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ได้แล้วครับ แต่ผมใช้วิธีแบบนี้
Code (JavaScript)
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText.substring(658,659)=='N'){
window.alert("dfas");
}
window.location.reload();
}
}
ไม่ทราบว่ามีวิธีที่ดีกว่านี้ไม่ครับ เนะนำด้วยนะครับ T^T
|
 |
 |
 |
 |
Date :
2011-12-19 02:08:45 |
By :
forfunza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณลอง alert(HttPRequest.responseText); ดูครับ ทำไมมันส่งอะไรมาเยอะแยะครับถึงจะต้องตัดเอา
|
 |
 |
 |
 |
Date :
2011-12-19 06:31:14 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?
include("include/connect.php");
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.tablesorter.js"></script>
<script type="text/javascript" src="js/pager/jquery.tablesorter.pager.js"></script>
<link rel="stylesheet" type="text/css" href="css/style111.css"/>
<link rel="stylesheet" type="text/css" href="Green Style/jquery-ui-1.8.16.custom.css"/>
<link rel="stylesheet" type="text/css" href="js/pager/jquery.tablesorter.pager.css"/>
<script type="text/javascript">
$(function(){
$("#myTable").tablesorter({widthFixed: true, widgets: ['zebra']})
.tablesorterPager({container: $("#pager")});
});
</script>
<?
$strMode = $_POST["tMode"];
$strID = $_POST["tID"];
if($strMode == "ADD")
{
$strSQL = "INSERT INTO animal_class ";
$strSQL .="(Class_Code,Class_Name,Phylum_Code) ";
$strSQL .="VALUES ";
$strSQL .="('','".$_POST["tName"]."','".$_POST["Code"]."')";
$objQuery = mysql_query($strSQL);
}
if($strMode == "UPDATE"){
$strSQL = "UPDATE animal_class SET ";
$strSQL .="Class_Name = '".$_POST["tName"]."' ";
$strSQL .=",Phylum_Code = '".$_POST["Code"]."' ";
$strSQL .="WHERE Class_Code = '".$_POST["tCode"]."' ";
$objQuery = mysql_query($strSQL);
}
if($strMode == "DELETE") {
$strSQL1 = " SELECT animal.Class_Code,animal_order.Class_Code FROM animal,animal_order WHERE animal.Class_Code=animal_order.Class_Code AND animal.Class_Code = '".$strID."' AND animal_order.Class_Code = '".$strID."' ";
$objQuery1 = mysql_query($strSQL1);
if($objResult1 = mysql_fetch_array($objQuery1))
{
echo "N";
}
else{
$strSQL = "DELETE FROM animal_class ";
$strSQL .="WHERE Class_Code = '".$strID."' ";
$objQuery = mysql_query($strSQL);
}
}
?>
<form name="frmSearch" method="get" action="AClass.php">
<table width="599" >
<tr>
<th>ค้นหา :
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="image" src="images/search-icon.png" value="submit" id="Find"></th>
</tr>
</table>
</form>
<?
$strSQL = "SELECT * FROM animal_class WHERE (Class_Name LIKE '%".$_GET["txtKeyword"]."%') ORDER BY Class_Code DESC";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table id="myTable" class="tablesorter">
<thead>
<tr height="40">
<th width="120"> <div align="center">รหัสคลาส</div></th>
<th> <div align="center">ชื่อคลาส</div></th>
<th> <div align="center">ชื่อไฟลัม</div></th>
<th width="80"> <div align="center">แก้ไข</div></th>
<th width="80"> <div align="center">ลบข่อมูล</div></th>
</tr>
</thead>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td align="center"><?=$objResult["Class_Code"];?></td>
<td align="center"><?=$objResult["Class_Name"];?></td>
<td align="center">
<?
$strSQL1 = "SELECT * FROM phylum WHERE Phylum_Code = '".$objResult["Phylum_Code"]."' ";
$objQuery1 = mysql_query($strSQL1);
while($objResult1 = mysql_fetch_array($objQuery1))
{
?>
<?=$objResult1["Phylum_Name"];?>
</td>
<td align="center"><a href="JavaScript:ShowEdit('<?=$objResult["Class_Code"];?>','<?=$objResult["Class_Name"];?>','<?=$objResult["Phylum_Code"];?>')"><img src="images/Icon-fix.png" width="20" height="23" /></a></td>
<td align="center"><a href="JavaScript:ajaxDelete('DELETE','<?=$objResult["Class_Code"];?>');" onClick="return confirm('ยืนยืนการลบข้อมูล <?=$objResult["Class_Name"];?>')"><img src="images/Dele.png" width="20" height="23" /></a></td>
</tr>
<?
}
}
?>
</table>
<div id="pager">
<form>
<img src="images/first.png" class="first" />
<img src="images/prev.png" class="prev"/>
<input type="text" class="pagedisplay" size="10"/>
<img src="images/next.png" class="next"/>
<img src="images/last.png" class="last"/>
<select class="pagesize">
<option selected="selected" value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</form>
</div>
<?
mysql_close($objConnect);
?>
ทั้ง หน้า ของ ผม ถ้า alert(HttPRequest.responseText); พวก ที่ไม่อยู่ใน php จะ ออกมาด้วยอะครับ ผมเลยต้องคัดเอา ไม่ทราบว่ามีวิธีอื่นอีกป่ะครับ T^T
|
 |
 |
 |
 |
Date :
2011-12-19 08:16:23 |
By :
forfunza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|