|
|
|
รบกวนด้วยคับ ajax การซื้อสินค้าโดยการถ้าซื้อสินค้าซ้ำกัน ให้มัน + จำนวนเอาอะคับ |
|
|
|
|
|
|
|
ajax1
code ::
Code (PHP)
<?
session_start();
if($_SESSION['UserID'] == "")
{
echo "Please Login!";
exit();
}
if($_SESSION['Status'] != "USER")
{
echo "This page for User only!";
exit();
}
mysql_connect(สงวน);
mysql_select_db("สงวน");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM member WHERE UserID = '".$_SESSION['UserID']."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
?>
<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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<title></title>
<link rel="stylesheet" href="style1.css" type="text/css" />
</head>
<body onLoad="JavaScript:doCallAjax('','')">
<div id="wrapper">
<div id="header">
<div id="logo">
</div>
<div id="updates"></div>
<div id="login">
<div id="loginwelcome"></div>
UserID: <?=$objResult["Username"];?>
Name:<?=$objResult["Name"];?>
<br>
<a href="edit_profile.php">Edit</a><a href="logout.php">Logout</a>
</div>
<ul id="navigation">
<li><a href="user_page.php">หน้าแรก</a></li>
<li class="on"><a href="AjaxPHPShoppingCart1.php">สินค้า</a></li>
<li><a href="detailproductuser.php">แก้ไขการสั่งซื้อ</a></li>
<li><a href="okorderproductuser.php">ประวัติการสั่งซื้อ</a></li>
<li><a href="check_sendproductuser.php">ประวัติการส่งสินค้า</a></li>
<li><a href="check_moneyuser.php">ประวัติการชำระเงิน</a></li>
</ul>
<div id="content">
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>ชื่อสินค้า หรือ ชนิดของสินค้าที่ต้องการค้นหา
<input name="search" type="text" id="search" value="<?=$_GET["search"];?>">
<input type="submit" value="Search"> </th>
</tr>
</table>
</form>
<table width="680" border="0" cellspacing="0" cellpadding="0">
<tr>
<tr>
<td width="243" valign="top">
<?
if($_GET["search"] != "")
{
$objConnect = mysql_connect("สงวน") or die("Error Connect to Database");
$objDB = mysql_select_db("สงวน");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM product WHERE (ProductName LIKE '%".$_GET["search"]."%' or type LIKE '%".$_GET["search"]."%' )";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
}
else
$strSQL = "SELECT * FROM product ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="879" border="1" cellpadding="1" cellspacing="1">
<tr>
<th width="97"> <div align="center">รูปสินค้า</div></th>
<th width="148"> <div align="center">ชื่อสินค้า</div></th>
<th width="87">ราคา</th>
<th width="124">จำนวนที่ต้องการ</th>
<th width="389" rowspan="2"><span id="mySpan"></span></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
$intRows++;
?>
<tr>
<td><img src="product/<?=$objResult["Picture"];?>" width="70" height="61" border="0"></td>
<td><center><?=$objResult["ProductName"];?></center></td>
<td><center><?=$objResult["Price"];?></center></td>
<script language="JavaScript">
function chkNumber(ele)
{
var vchar = String.fromCharCode(event.keyCode);
if ((vchar<'0' || vchar>'9') && (vchar != '.')) return false;
ele.onKeyPress=vchar;
}
</script>
<td><center><input type="text" id="txt<?=$intRows;?>" value="" style="width:20px" OnKeyPress="return chkNumber(this)">
<input type="button" value="Add" onClick="JavaScript:doCallAjax('<?=$objResult["ProductID"];?>',document.getElementById('txt<?=$intRows;?>').value);"></center></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<div id="footer">
<div id="copyright">© 2008 All Rights Reserved. Designed by Thanyapat wiriyathanachot</a>.
</div>
</div>
</body>
</html>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2013-02-19 18:08:25 2013-02-19 18:14:19
|
|
|
|
|
Date :
2013-02-19 18:07:38 |
By :
waffle |
View :
884 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ajax2
code ::
Code (PHP)
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
session_start();
$strProductID = $_POST["tProductID"];
$strQty = $_POST["tQty"];
$objConnect = mysql_connect("สงวน") or die("Error Connect to Database");
$objDB = mysql_select_db("สงวน");
mysql_query("SET NAMES UTF8");
$sql_sel = "SELECT COUNT(*) FROM cart WHERE SID = '".session_id()."' AND ProductID = '".$strProductID."'";
$query_sel = mysql_query($sql_sel);
$count_sel = mysql_num_rows($query_sel);
echo"$sql_sel";
echo"$count_sel";
if($count_sel >0){
$strSQL = "UPDATE cart SET ";
$strSQL .="Qty = '".$strQty."' ";
$strSQL .="WHERE ";
$strSQL .="SID = '".session_id()."' AND ProductID = '".$strProductID."'";
$objQuery = mysql_query($strSQL);
}else {
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>ใบสั่งซื้อ</h1>
<table width="501" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="51"><div align="center">ลำดับที่</div></td>
<td width="154" height="26"><div align="center">สินค้า</div></td>
<td width="69"><div align="center">ราคา</div></td>
<td width="57"><div align="center">จำนวน</div></td>
<td width="65"><div align="center">ราคาสุทธิ</div></td>
<td width="32"><div align="center">ลบ</div></td>
<td width="32">แก้ไข</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="center"><?=number_format($objResultPro["Price"],2);?></div></td>
<td><div align="center"><?=$objResult["Qty"];?></div></td>
<td><div align="right">
<div align="center"> <?=number_format($intTotal,2);?>
</div></td>
<td><center><a href="delete.php?del=<?=$objResultPro["ProductID"];?>">(x)</a></center></td>
<td><a href="editItem.php?item=<?=$objResultPro["ProductID"];?>">(x)</a></td>
</tr>
<?
}
?>
<tr>
<td colspan="4"><div align="center">รวมราคา</div></td>
<td><div align="center"><?=number_format($intSumTotal,2);?></div></td>
<td> </td>
<td><div align="c"></div></td>
</tr>
</table>
<?
if($intSumTotal > 0)
{
?>
<br><input name="btnCheckOut" type="submit" id="btnCheckOut" value="ตกลง" onClick="JavaScript:CheckOut();">
<?
}
?>
</center>
|
|
|
|
|
Date :
2013-02-19 18:08:39 |
By :
waffle |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แนวทางครับ เป็นโค้ดของผมที่ดัดแปลง จากบทความของที่นี้ครับผม
Code (PHP)
$oldproid = "";
$_SESSION['SID'] = $anspsub;
$_SESSION['CUS_ID'] = $_POST['tCID'];
$proidsale = $_POST['tProductID'];
$qtysale = $_POST['tQty'];
$price = $_POST['tPrice'];
//////////////////select ถ้าสินค้ามีอยูแล้วให้เพิ่มเข้าไป///////////////////////////
$ss = "select pro_id,sale_qty from saledetail where pro_id = '$proidsale' and s_id = '$anspsub'";
//echo $ss;
$resultss = mysql_query($ss);
if(mysql_num_rows($resultss) == 1 ){
$old=mysql_fetch_array($resultss);
$oldproid = $old['pro_id'];
$oldqty = $old['sale_qty'];
}
//////select product qty
$proqty = "select pro_qty from product where pro_id = '$proidsale' ";
//echo $proqty;
$resultproqty = mysql_query($proqty);
if(mysql_num_rows($resultproqty) == 1 ){
$pqty=mysql_fetch_array($resultproqty);
$qtypro = $pqty['pro_qty'];}
//////////////////////////////////////////////////////////////////////////////////////////
if($proidsale != "" and $qtysale != "")
{
if($qtysale<=$qtypro){
$newqty = 0;
if($proidsale == $oldproid){//ถ้ามีสินค้าในรายการอยู่แล้วให้เพิ่มเข้าไป
$newqty = $qtysale + $oldqty;
$total = $price*$newqty;
$updatesale = "update saledetail set sale_qty = $newqty ,total = $total where pro_id = '$proidsale' and s_id = '$anspsub'";
mysql_query($updatesale);
$updateqtyproduct= "update product set pro_qty = $qtypro - $qtysale where pro_id = '$proidsale'";
//echo $updateqtyproduct;
mysql_query($updateqtyproduct);
}
else{//ถ้ไม่มี ADD ใหม่
$total = $price*$qtysale;
$insertsale = "insert into saledetail(s_id,pro_id,sale_qty,price,total) values('$anspsub','$proidsale',$qtysale,$price,$total)";
mysql_query($insertsale);
$updateqtyproduct= "update product set pro_qty = $qtypro - $qtysale where pro_id = '$proidsale'";
//echo $updateqtyproduct;
mysql_query($updateqtyproduct);
}
|
ประวัติการแก้ไข 2013-02-19 20:06:47 2013-02-19 20:08:04 2013-02-19 20:08:29
|
|
|
|
Date :
2013-02-19 20:05:56 |
By :
nutzaaclub |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณคับ แกะ code สักครู่ ^^
|
|
|
|
|
Date :
2013-02-19 20:13:02 |
By :
waffle |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|