|
 |
|
ผมทำหน้า AjaxPHPShoppingCart อะครับอยากให้มีปุ่ม Del หน้าตารางการของใส่อะครับ |
|
 |
|
|
 |
 |
|
อยากให้มีปุ่ม Del หน้าของที่เราซื้ออะครับต้องแทรก code ตรงไหนหรอครับ
กับผมเจอปัญหาเวลามี User 1 คนมาหยิบใส่ตะกร้า ปุ๊บ User คนที่ 2 มาหยิบอีกมันจะไปเข้าตะกร้าคนที่ 1 ด้วยอะครับ

Code (PHP)
<?php
ob_start();
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$strid_pro = $_POST["tid_pro"];
$strQty = $_POST["tQty"];
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("admin");
if($strid_pro != "" and $strQty != "")
{
$strSQL = "INSERT INTO cart ";
$strSQL .="(SID,id_pro,Qty) ";
$strSQL .="VALUES ";
$strSQL .="('".session_id()."','".$strid_pro."','".$strQty."') ";
$objQuery = mysql_query($strSQL);
}
?>
<center>
<h1>Your Cart</h1>
<table width="408" 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>
</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 ++;
$strSQL = "SELECT * FROM product WHERE id_pro = '".$objResult["id_pro"]."' ";
$objQueryPro = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResultPro = mysql_fetch_array($objQueryPro);
$intTotal = $objResult["Qty"] * $objResultPro["price_pro"];
$intSumTotal = $intSumTotal + $intTotal;
?>
<tr>
<td><div align="center"><?=$intRows;?></div></td>
<td><?=$objResultPro["name_pro"];?></td>
<td><div align="right"><?=number_format($objResultPro["price_pro"],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="Check Out" onClick="JavaScript:CheckOut();">
<?
}
?>
</center>
</body>
</html>
<? ob_end_flush() ?>
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2011-02-07 13:36:14 |
By :
thegunmanolo |
View :
991 |
Reply :
10 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
table cart ไม่มี PK แล้วจะลบข้อมูลอย่างไรล่ะครับ
สำหรับเพิ่มบล ก็
ที่
<tr>
<td width="51"><div align="center">Del</div></td>
<td width="51"><div align="center">ID</div></td>
...
</tr>
ในลูปก็แค่เพิ่ม
<tr>
<td><div align="center"> ปุ่มลบ </div></td>
<td><div align="center"><?=$intRows;?></div></td>
..
</tr>
ในปุ่มลบคุณก็ต้องเขียน ajax สำหรับลบสินค้าเอาเองนะ โดยต้องส่ง PK ของตารง cart ไปด้วยจะได้รู้ว่าต้องการลบอันไหน หรือถ้าจะไม่ใช้ PK ก็ไปใช้ รหัสสินค้าแทนแล้วกัน ขึ้นอยู่กับการออกแบบ ของคุณทั้งสิน 
|
 |
 |
 |
 |
Date :
2011-02-07 17:04:59 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับผมจำนำไปทำดูครับ
|
 |
 |
 |
 |
Date :
2011-02-07 17:19:11 |
By :
thegunmanolo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองดัดแปลงเอาอะครับแต่มันติด error อะครับไม่รู้ว่าถูกไหม
Parse error: syntax error, unexpected $end in C:\AppServ\www\DeleteRecord.php on line 124
DeleteRecord.php
Code (PHP)
<html>
<head>
<title>DVDTHAILAND</title>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(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 = 'ShoppingCart2.php';
var pmeters = "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 == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
document.getElementById("tr"+ID).style.display = 'none';
}
}
}
}
</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","root") or die("Error Connect to Database");
$objDB = mysql_select_db("admin");
$strSQL = "SELECT * FROM product ORDER BY id_pro 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["pic_pro"];?>" width="70" height="61" border="0">
<center>
<?=$objResult["name_pro"];?>
<br>
<?=$objResult["price_pro"]." Baht";?>
<br>
<input type="text" id="txt<?=$intRows;?>" value="" style="width:20px">
<input type="button" value="Add" onClick="JavaScript:doCallAjax('<?=$objResult["id_pro"];?>', document.getElementById('txt<?=$intRows;?>').value);">
</center>
<h1>Your Cart</h1>
<table width="408" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="51"><div align="center">Del</div></td>
<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>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><a href="JavaScript:doCallAjax('<?=$objResult["id_pro"];?>');">Del</a></div></td>
<td><div align="center"><?=$intRows;?></div></td>
<td><?=$objResultPro["name_pro"];?></td>
<td><div align="right"><?=number_format($objResultPro["price_pro"],2);?></div></td>
<td><div align="center"><?=$objResult["Qty"];?></div></td>
<td><div align="right"><?=number_format($intTotal,2);?></div></td>
</tr>
<?
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>
DeleteRecord4.php
Code (PHP)
<?php
$strID = $_POST["tID"];
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "DELETE FROM product ";
$strSQL .="WHERE id_pro = '".$strID."' ";
$objQuery = mysql_query($strSQL);
if($objQuery)
{
echo "Y";
}
else
{
echo "N";
}
mysql_close($objConnect);
?>
|
ประวัติการแก้ไข 2011-02-09 00:46:07 2011-02-09 00:46:58
 |
 |
 |
 |
Date :
2011-02-09 00:45:31 |
By :
thegunmanolo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขออนุญาติดันนะครับ ขอบคุณครับ
รบกวนทีครับ
|
 |
 |
 |
 |
Date :
2011-02-11 08:48:39 |
By :
thegunmanolo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มี while loop มาเกินหรือเปล่าครับ
|
 |
 |
 |
 |
Date :
2011-02-11 12:54:19 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
while loop แรกก็ โชว์ My Cart
while loop สองก็โชว์ Your Cart
ผมเข้าใจอะไรผิดปะครับ
ถ้าผิดไปขอโทษทีครับ
|
 |
 |
 |
 |
Date :
2011-02-11 14:10:09 |
By :
thegunmanolo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขออนุญาติดันนะครับ
คือติดตรง del ข้อมูลของตะกร้าสินค้าอะครับ
|
 |
 |
 |
 |
Date :
2011-02-12 13:44:04 |
By :
thegunmanolo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ติดแค่ลบใช่มั้ยครับ add product ไม่มีปัญหาใช่มั้ย
ถ้าเป็นโค้ดที่คุณให้มานั้น มันขาดจุดสิ้นสุดของ loop ตัวแรก ก็คือคุณตก } ไปตัวนึง
ส่วนใน deleterecord.php ก็ควรจะใส่ if ตรวจสอบนิดนึงว่าได้ส่งค่า tid มาหรือเปล่า ไม่ควรให้ทำคำสั่งลบในทันที
ผมดูโค๊ดแล้วก็งงนะ ไม่รู้ว่าทำไมคุณเขียน My Cart กับ Your Cart โดยใช้ตารางเดียวกัน sql ตัวเดียวกัน
ซึ่งที่จริง My Cart จะต้องเป็น product ซึ่งตอนนี้ก็ถูกต้องแล้วแค่เพียงคุณเขียนว่า My Cart เท่านั้นอันนี้แล้วแต่
ส่วน Your Cart นั้นเป็นส่วนที่ผู้ใช้ซื้อสินค้า จับสินค้าใส่ตะกร้า ฉะนั้นมันก็ควรที่จะดึงมาจากตารางตะกร้าสินค้าของลูกค้าคนนั้นถึงจะถูก
อันนี้ผมไม่รู้นะว่าคุณออกแบบมายังไง
แล้วตอนนี้มันติด Error อะไรหรอ? ในส่วนลบข้อมูล
|
 |
 |
 |
 |
Date :
2011-02-12 23:34:21 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าให้ผมแนะนำมันจะเป็นแบบนี้ (ตย. ผมใช้ jQuery เอานะมันเขียนโค้ดสั้นดี และเป็นแบบสมาชิกล็อกอินแล้ว)
Code (product.php)
<?
session_start();
?>
<html><head><title>test</title>
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
//หยิบสินค้าใส่ตะกร้า (รหัสสินค้า, รหัสสมาชิก)
//เอามาแสดงที่ displayCart
function addToCart(pId,mId) {
$.ajax({
type: "POST",url: "read_data.php",data: "action=addProduct&id="+pId+"&mId="+mId+"&dumy="+Math.random(),timeout: 8000,dataType: "text/html",
error: function(){alert('Loading time out');},
beforeSend: function(){$("#displayCart").html("<img src='images/loading.gif'>");},
success: function(html){$("#displayCart").html(html);}
});
}
//ลบสินค้าออกจากตะกร้า (รหัสสินค้า,รหัสสมาชิก)
function deleteProduct(pId,mId){
$.ajax({
type: "POST",url: "read_data.php",data: "action=delProduct&pId="+id+"&mId="+mId+"&dumy="+Math.random(),timeout: 8000,dataType: "text/html",
error: function(){alert('Loading time out');},
beforeSend: function(){$("#displayCart").html("<img src='images/loading.gif'>");},
success: function(html){$("#displayCart").html(html);}
});
}
//แสดงสินค้า (รหัสสมาชิก)
function listProduct(mId){
$.ajax({
type: "POST",url: "read_data.php",data: "action=listProduct&pId="+id+"&mId="+mId+"&dumy="+Math.random(),timeout: 8000,dataType: "text/html",
error: function(){alert('Loading time out');},
beforeSend: function(){$("#displayCart").html("<img src='images/loading.gif'>");},
success: function(html){$("#displayCart").html(html);}
});
}
</script>
</head>
<body>
<table boder="0" width="600">
<tr><td width="220">
$strSQL = "SELECT * FROM product ORDER BY id_pro 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++;
?>
<div style="float:left; width:100px; height:110px; margin:4px; text-align:center;">
<img src="../images/<?=$objResult["pic_pro"];?>" width="70" height="61" border="0">
<?=$objResult["name_pro"];?><br>
<?=$objResult["price_pro"]." Baht";?><br>
<input type="text" id="txt<?=$intRows;?>" value="" style="width:20px">
<input type="button" value="Add" onClick="addToCart('<?=$objResult["id_pro"];?>', '<?=$_SESSION["member_id"];?>');">
</div>
<?
}
?>
</td>
<td><div id="displayCart"><script language="javascript"> listProduct('<?=$_SESSION["member_id"];?>');</script></div></td>
</tr>
</table>
</body>
</html>
Code (read_data.php)
session_start();
include("connect.php"); //connect database
function listCart($mId){
?>
<h1>Your Cart</h1>
<table width="408" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="51"><div align="center">Del</div></td>
<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>
</tr>
<?
$sql select * from product where mId=".$_POST["mId"].";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mysql_fetch_array($objQuery)){
?>
<tr>
<td><div align="center"><a href="JavaScript:delProduct('<?=$objResult["id_pro"];?>','<?=$_SESSION["member_id"]?>');">Del</a></div></td>
<td><div align="center"><?=$intRows;?></div></td>
<td><?=$objResultPro["name_pro"];?></td>
<td><div align="right"><?=number_format($objResultPro["price_pro"],2);?></div></td>
<td><div align="center"><?=$objResult["Qty"];?></div></td>
<td><div align="right"><?=number_format($intTotal,2);?></div></td>
</tr>
<?
}
}
if($_POST["action"]=="listProduct" && isset($_POST["mId"])){
listCart($_POST["mId"]);
}else if($_POST["action"]=="addProduct" && isset($_POST["mId"])){
//sql add product
listCart($_POST["mId"]);
}else if($_POST["action"]=="delProduct" && isset($_POST["mId"]) && isset($_POST["pId"])){
//sql delete product
listCart($_POST["mId"]);
}
|
ประวัติการแก้ไข 2011-02-13 00:40:24 2011-02-13 00:42:57
 |
 |
 |
 |
Date :
2011-02-13 00:39:24 |
By :
chineji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมจะลองทำตามดูนะครับ ผมไม่อยากรบกวนเลยอะครับ ช่วยผมมากจริงๆ ผมเกรงใจจริงๆครับ
ขอบคุณมากๆครับผมติดตรงนี้จริงๆๆครับ
|
ประวัติการแก้ไข 2011-02-13 05:14:05
 |
 |
 |
 |
Date :
2011-02-13 05:12:16 |
By :
thegunmanolo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|