|
|
|
ajax e-commerce เพิ่มเติมการ Delete โดยใช้ http://www.thaicreate.com/tutorial/ajax-shopping-cart.html เป็นแบบ |
|
|
|
|
|
|
|
เอาcodeที่เขียนมาดูสิครับ
|
|
|
|
|
Date :
2013-01-12 23:13:51 |
By :
matay107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (AjaxPHPShoppingCart1.php)
<html>
<head>
<title></title>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(ProductID,Qty) {
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 = 'AjaxPHPShoppingCart2.php';
var pmeters = "tProductID=" + ProductID+
"&tQty=" + Qty;
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;
}
}
}
function CheckOut()
{
window.location = 'AjaxPHPShoppingCart3.php';
}
</script>
</head>
<body onLoad="JavaScript:doCallAjax('','')">
<h1>My Cart </h1>
<table width="680" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="234" valign="top">
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("rakbanlohaphan");
mysql_query("Set names 'tis620'");
$strSQL = "SELECT * FROM product ORDER BY ProductID ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
echo"<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\"><tr>";
$intRows = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
echo "<td>";
?>
<img src="product/<?=$objResult["Picture"];?>" width="70" height="61" border="0">
<center>
<?=$objResult["ProductName"];?>
<br>
<?=$objResult["Price"]." Baht";?>
<br>
<input type="text" id="txt<?=$intRows;?>" value="" style="width:20px">
<input type="button" value="Add" onClick="JavaScript:doCallAjax('<?=$objResult["ProductID"];?>',document.getElementById('txt<?=$intRows;?>').value);">
</center>
<?
echo"</td>";
if(($intRows)%2==0)
{
echo"</tr>";
}
else
{
echo "<td>";
}
}
echo"</tr></table>";
?>
<?
mysql_close($objConnect);
?>
</td>
<td width="446" valign="top"><span id="mySpan"></span></td>
</tr>
</table>
<p> </p>
</body>
</html>
|
|
|
|
|
Date :
2013-01-13 00:23:50 |
By :
waffle |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (AjaxPHPShoppingCart2.php)
<?php
session_start();
$strProductID = $_POST["tProductID"];
$strQty = $_POST["tQty"];
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("rakbanlohaphan");
mysql_query("Set names 'utf8'");
if($strProductID != "" and $strQty != "")
{
$strSQL = "INSERT INTO cart ";
$strSQL .="(SID,ProductID,Qty) ";
$strSQL .="VALUES ";
$strSQL .="('".session_id()."','".$strProductID."','".$strQty."') ";
$objQuery = mysql_query($strSQL);
}
?>
<center>
<h1>Your Cart</h1>
<table width="475" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="51"><div align="center">ID</div></td>
<td width="154" height="26"><div align="center">Product</div></td>
<td width="69"><div align="center">Price</div></td>
<td width="57"><div align="center">Qty</div></td>
<td width="65"><div align="center">Total</div></td>
<td width="65"> delete</td>
</tr>
<?
$intSumTotal = 0;
$intRows = 0;
$strSQL = "SELECT * FROM cart WHERE SID = '".session_id()."' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery))
{
$intRows ++;
//*** Product ***//
$strSQL = "SELECT * FROM product WHERE ProductID = '".$objResult["ProductID"]."' ";
$objQueryPro = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResultPro = mysql_fetch_array($objQueryPro);
$intTotal = $objResult["Qty"] * $objResultPro["Price"];
$intSumTotal = $intSumTotal + $intTotal;
?>
<tr>
<td><div align="center"><?=$intRows;?></div></td>
<td><?=$objResultPro["ProductName"];?></td>
<td><div align="right"><?=number_format($objResultPro["Price"],2);?></div></td>
<td><div align="center"><?=$objResult["Qty"];?></div></td>
<td><div align="right">
<?=number_format($intTotal,2);?>
</div></td>
<td><a href="delete.php?del=<?=$objResultPro["ProductID"];?>">(x)</a></td>
</tr>
<?
}
?>
<tr>
<td colspan="4"><div align="right">Total Amount </div></td>
<td><div align="right">
<?=number_format($intSumTotal,2);?>
</div></td>
<td> </td>
</tr>
</table>
<?
if($intSumTotal > 0)
{
?>
<br><input name="btnCheckOut" type="submit" id="btnCheckOut" value="Check Out" onClick="JavaScript:CheckOut();">
<?
}
?>
</center>
|
|
|
|
|
Date :
2013-01-13 00:24:45 |
By :
waffle |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมขอแนวที่จะ delete จาก AjaxPHPShoppingCart2.php
โดยกด (x) แล้ว ให้ลบ ออกจากตางที่เลือกมาทั้งหมดอะคับ
|
|
|
|
|
Date :
2013-01-13 00:25:51 |
By :
waffle |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|