|
|
|
สอบถามเรื่อง AJax Shopping Cart ค่ะ ใครทราบ ช่วยทีค่ะ คือหนูต้องทำโปรเจคส่งอาจารย์นะค่ะแล้ว |
|
|
|
|
|
|
|
ใครทราบ ช่วยทีค่ะ คือหนูต้องทำโปรเจคส่งอาจารย์นะค่ะแล้ว ได้นำโค้ดของ Ajax Shopping cart ไปลองเขียนดูทำตามทุกอย่างเลย แต่ทำไมมันคำนวณได้แค่บรรทัดเดียวค่ะ คือadd ข้อมูลได้แค่อันเดียว พอadd อีกที มันไม่ขึ้นน่ะค่ะไม่เข้าใจงงมากเลยพึ่งหัดเขียนนะค่ะ หรือว่าเขียนข้อมูลผิด ช่วยดูให้หน่วยค่ะ
//ส่วนของฐานข้อมูลค่ะ//
ตารางproduct
CREATE TABLE `product` (
`ProductID` int(12) NOT NULL auto_increment,
`ProductName` varchar(36) NOT NULL,
`Price` decimal(12,2) NOT NULL,
`Picture` varchar(100) NOT NULL,
PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM DEFAULT CHARSET=tis620 AUTO_INCREMENT=1239902849 ;
--
-- Dumping data for table `product`
--
INSERT INTO `product` VALUES (1239902845, 'Vitamilk', 10.00, '1239902845');
INSERT INTO `product` VALUES (1239902846, 'Cokezero', 15.00, '1239902846');
INSERT INTO `product` VALUES (1239902847, 'Lactasoy', 10.00, '1239902847');
INSERT INTO `product` VALUES (1239902848, 'Pantene', 75.00, '1239902848');
------------------------------------------------------------------------------------------------------------------------------
ตารางcart`
CREATE TABLE `cart` (
`SID` int(12) NOT NULL,
`ProductID` int(12) NOT NULL,
`Qty` int(32) NOT NULL,
PRIMARY KEY (`SID`)
) ENGINE=MyISAM DEFAULT CHARSET=tis620;
//ส่วนของโค้ด php ค่ะ//
ชื่อ AjaxPHPShoppingCart1.php
<?php
?>
<html>
<head>
<title>Point of sale</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>
<link href="AjaxPHPShoppingCart2.php" rel="stylesheet" type="text/css">
</head>
<body onLoad="JavaScript:doCallAjax('','')">
<h2>ตัวอย่างสินค้า</h2>
<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("mydatabase");
$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="images/<?=$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 name="Submit" type="submit" onClick="JavaScript:doCallAjax('<?=$objResult["ProductID"];?>', document.getElementById('txt<?=$intRows;?>').value);" value="Add">
</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>
</body>
</html>
-----------------------------------------------php ส่วนที่2----------------------
ชื่อ 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("pos");
if($strProductID != "" and $strQty != "")
{
$strSQL = "INSERT INTO cart ";
$strSQL .="(SID,ProductID,Qty) ";
$strSQL .="VALUES ";
$strSQL .="('".session_id()."','".$strProductID."','".$strQty."') ";
$objQuery = mysql_query($strSQL);
}
?>
<center>
<h2>Point of sale</h2>
<table width="408" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="51"><div align="center">Number</div></td>
<td width="154" height="26"><div align="center">Name</div></td>
<td width="69"><div align="center">Price</div></td>
<td width="57"><div align="center">Qyt</div></td>
<td width="65"><div align="center">Total</div></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>
</tr>
<?
}
?>
<tr>
<td colspan="4"><div align="right">Total Amount </div></td>
<td>
<div align="right"><?=number_format($intSumTotal,2);?></div></td>
</tr>
</table>
<?
if($intSumTotal > 0)
{
?>
<br><input name="btnCheckOut" type="submit" id="btnCheckOut" value="Recive" onClick="JavaScript:CheckOut();">
<?
}
?>
</center>
ช่วยทีค่ะพี่ๆ จะร้องไห้แล้ว ไม่รู้เรื่องจริงๆค่ะ
Tag : - - - -
|
|
|
|
|
|
Date :
2009-11-01 17:11:16 |
By :
poopeit10 |
View :
941 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ในตัวอย่างมันก็ขึ้นปกติน่ะครับ
|
|
|
|
|
Date :
2009-11-01 19:15:51 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|