|
|
|
ช่วยหน่อยนะคะ มีปัญหาเกี่ยวกับระบบ addcart อ่าค่ะลอกโค้ดมากจากหนังสือ แต่กดปุ่ม addcart แล้วมันไม่มีไรเกิดขึ้นเรยค่าาาา |
|
|
|
|
|
|
|
นี่เปนโค้ดหน้าหลักที่จะมีปุ่มให้กด addcart นะคะ
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<script type="text/javascript" src="myjs.js">
</script>
<script type="text/javascript" src="framework.js"> // framework เป็นโค้ดที่ในหนังสือสร้างขึ้นมาเพื่อเปนตัวทำ ajax อ่าค่ะ
</script>
<script>
function addCart(id){ //function นี้ให้ถูกเรียกเม่อกด addcart อ่าค่ะ
var data = "id=" + id;
var url = "addCart.php";
ajaxLoad('post', url, data, "cart");
}
function readCart() {
ajaxLoad('post', "read_cart.php", null, "cart");
}
</script>
</head>
<body>
<div>
<? include('menubar.php'); ?>
</div>
<? include('onlineshop.inc.php');?>
<table width="1000" align="center" border="0">
<tr>
<td align="left"> <? include('sidemenu.php');?>
<table id="cart" bgcolor="#FF0066">
<tr> <td> <script> readCart(); </script> </td></tr>
</table>
</td>
<td>
<table width="100%" border="0">
<tr> <td width="30%"> </td>
<td width="50%"> </td>
<td> </td>
</tr>
<?php include("paging.inc.php");
$current_page=1;
if(isset($_GET['page']))
{
$current_page=$_GET['page'];
}
$rows_per_page = 9;
$start_row = paging_start_row($current_page,$rows_per_page);
$keyword=$_GET["search"];
if($keyword != "")
{
$argId=$_GET["search"];
$sql = "select distinct (Product_Name)from product as p,wearer as w ,category as t, brand as b,Color as c,material as m,size as s
where p.Product_Name like '%".$argId."%'
or p.Product_Prices like '%".$argId."%'
or p.Product_Des like '%".$argId."%'
or (w.Wearer_Description like '%".$argId."%'and w.Wearer_Id=p.Wearer_Id )
or (w.Wearer_Id=p.Wearer_Id and w.Wearer_Type like '%".$argId."%' )
or (b.Brand_Id=p.Brand_Id and b.Brand_Name like '%".$argId."%' )
or (t.Cat_Id=p.Cat_Id and t.Cat_Name like '%".$argId."%' )
or (c.Color_Id=p.Color_Id and c.Color_Name like '%".$argId."%')
or (m.Mat_Id=p.Mat_Id and m.Mat_Name like '%".$argId."%' );
or (s.Size_Description like '%".$argId."%' and s.Size_Id = p.Size_Id )
or (s.Size_Id=p.Size_Id and s.Size_Name like '%".$argId."%' )";
/*$sql= "SELECT SQL_CALC_FOUND_ROWS * from product, color,material, category WHERE ( (product.Product_Des LIKE '%".$argId."%') AND Size_Id='s1' LIMIT $start_row, $rows_per_page ;";*/
}
else
{
$argId=$_GET["argId"];
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM product WHERE (Cat_Id ='$argId' OR Color_Id='$argId' OR Wearer_Id='$argId' OR Mat_Id='$argId' OR Product_Des LIKE '%".$argId."%') AND Size_Id='s1' LIMIT $start_row, $rows_per_page;";
if(strcmp($argId,"EDITOR")==0 or strcmp($argId,"FALL ")==0 or strcmp($argId,"CHILL OUT")==0 or strcmp($argId,"OFFICE LOOK")==0)
{
$sqlpic="SELECT * FROM headpic WHERE pic_name ='$argId' ;"; //for headpic
//headpic section
$resultpic = mysql_query($sqlpic);
while($rowpic = mysql_fetch_array($resultpic))
{
$headpic=$rowpic['pic_pict'];
}
//headpic section
}
/*else
{
$sqlpic="SELECT headpic.pic_pict FROM headpic WHERE headpic.pic_name IN (SELECT category.cat_name, color.color_name, material.mat_name FROM category, color, material WHERE category.cat_Id='$argId' OR color.color_Id='$argId' OR material.mat_Id='$argId');";
//headpic section
$resultpic = mysql_query($sqlpic);
while($rowpic = mysql_fetch_array($resultpic))
{
$headpic=$rowpic['pic_pict'];
}
//headpic section
} */
}
$result=mysql_query($sql);
$found_rows=mysql_query("SELECT FOUND_ROWS();");
$total_rows=mysql_result($found_rows,0,0);
$total_pages = paging_total_pages($total_rows, $rows_per_page);
if($total_rows==0){
echo "<caption><b> no result </b></caption>";
}
/*else {
$stop_row = paging_stop_row($start_row, $rows_per_page, $total_rows);
echo "<caption><b> product no.". ($start_row +1) . "-" . "$stop_row from total $total_rows </b></caption>";
} */
$count=0;
echo "<table border=0 align=center >";
echo "<tr><td colspan=3><div> <img src='$headpic'></div> </td></tr>"; //for headpic
while($objResult = mysql_fetch_array($result))
{
$PID = $objResult['Product_Id'];
$Pname = $objResult['Product_Name'];
$Pdesc1 = $objResult['Product_Des'];
$Pdesc = substr($Pdesc1,0,80). "..." ;
$Pprice = $objResult['Product_Prices'];
$Ppic = $objResult['Product_Pict'];
if($count==0)
{
echo "<tr> <td width=250 ><div id=product> <img width=140 src='$Ppic?Product_Id=$PID'> </div>";
echo "<div id=productName> <br><b> <a href='product_detail.php?PID=$PID'> $Pname </a> </b></div>";
echo "<div id=product> <br> $Pdesc </div>";
echo "<div id=product> <br> $Pprice Baht</div>";
echo "<div id=product> <br> <input type=button value='add to shopping bag' onclick='addCart($PID)' > </div>";
echo "</td>";
$count++;
}
else if($count==1)
{
echo "<td width=250 > <div id=product> <img width=140 src='$Ppic?Product_Id=$PID'> </div>";
echo "<div id=productName> <br><b><a href='product_detail.php?PID=$PID'> $Pname </a> </b> </div>";
echo "<div id=product> <br> $Pdesc </div>";
echo "<div id=product> <br> $Pprice Baht</div>";
echo "<div id=product> <br> <input type=button value='add to shopping bag' onclick='addCart($PID)' > </div>"; //addcart
echo "</td>";
$count++;
}
else if($count==2)
{
echo "<td width=250 > <div> <img width=140 src='$Ppic?Product_Id=$PID'> </div>";
echo "<div id=productName> <br><b><a href='product_detail.php?PID=$PID'> $Pname </a> </b> </div>";
echo "<div id=product> <br> $Pdesc </div>";
echo "<div id=product> <br> $Pprice Baht</div>";
echo "<div id=product> <br> <input type=button value='add to shopping bag' onclick='addCart($PID)' > </div>";
echo "</td>";
echo "</tr>";
$count=0;
}
} echo "</table>";
?>
<p align="right"> <br />
<?php
$page_range=5;
$qry_str="argId=$argId";
//$total_pages = paging_total_pages($total_rows, $rows_per_page);
$pagenum = paging_pagenum($current_page, $total_pages, $page_range, $qry_str);
?>
<?
echo "page:" . $pagenum;
?>
</p>
</table>
</td>
</tr>
</table>
<div>
<? include('footer.php'); ?>
</div>
</body>
</html>
อันนี้เปนโค้ด framework ค่ะ ที่ลอกมาจากหนังสือเรย
Code (PHP)
function ajaxLoad(method, URL, data, displayId) {
var AJAX = null;
if(window.ActiveXObject) {
AJAX = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) {
AJAX = new XMLHttpRequest();
}
else {
alert("Your browser doesn't support AJAX");
return;
}
method = method.toLowerCase();
URL += "?dummy=" + (new Date()).getTime();
if(method=="get") {
URL += "&" + data;
data = null;
}
AJAX.open(method, URL);
if(method=="post") {
AJAX.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
AJAX.onreadystatechange = function() {
if(AJAX.readyState==4 && AJAX.status==200) {
var ctype = AJAX.getResponseHeader("Content-Type").toLowerCase();
ajaxCallback(ctype, displayId, AJAX.responseText);
delete AJAX;
AJAX = null;
}
}
AJAX.send(data);
}
function ajaxCallback(contentType, displayId, responseText) {
if(contentType.match("text/javascript")) {
eval(responseText);
}
else {
if(displayId==null) {
return;
}
var el = document.getElementById(displayId);
el.innerHTML = responseText;
}
}
function getFormData(form_name_or_id) {
var frm = document.forms[form_name_or_id];
if(frm==null) {
alert("Form: '" + form_name_or_id + "' not found!");
return;
}
var data = "";
var num_el = frm.elements.length;
for(i=0; i<num_el; i++) {
var el = frm.elements[i];
if(el.name=="" && el.id=="") {
continue;
}
var param_name = "";
if(el.name!="") {
param_name = el.name;
}
else if(el.id!="") {
param_name = el.id;
}
var t = frm.elements[i].type;
var value = "";
if(t=="text"||t=="password"||t=="hidden"||t=="textarea") {
value = encodeURIComponent(el.value);
}
else if(t=="radio"||t=="checkbox") {
if(el.checked) {
value = encodeURIComponent(el.value);
}
else {
continue;
}
}
else if(t=="select-one") {
value = encodeURIComponent(el.options[el.selectedIndex].value);
}
else if(t=="select-multiple") {
for(j=0; j<el.length; j++) {
if(el.options[j].selected) {
if(data!="") {
data += "&";
}
data += param_name + "=";
data += encodeURIComponent(el.options[j].value);
}
}
continue;
}
if(data!="") {
data += "&";
}
data += param_name + "=" + value;
}
return data;
}
ข้างล่างนี้เปนโค้ด addcart ปกติอ่ะค่ะ ตรงนี้ไม่น่าจาผิดนะคะ แต่ไม่รุว่าผิดตรงไหนอ่าค่ะ
Code (PHP)
<?php
session_start();
$SID = session_id();
$PID="";
if(isset($_POST['id']))
{
$PID = $_POST['id'];
}
else {
exit;
}
include("onlineshop.inc.php");
$sql = "SELECT * FROM product WHERE Product_Id = $PID;";
$result = mysql_query($sql);
$Pname = mysql_result($result,0,0);
$Pprice = mysql_result($result,0,1);
//add detail into table cart using REPLACE just in case that there r duplicate adding items
$sql = "REPLACE INTO cart VALUES('$SID', '$PID', '$Pname', '$Pprice', 1, NOW());";
mysql_query($sql);
echo "alert('added');
readCart();";
?>
รบกวนด้วยนะคะ
Tag : PHP, Ms Access
|
|
|
|
|
|
Date :
2012-01-01 19:06:15 |
By :
gellarjung |
View :
999 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าไม่มี error แสดงออกมาหน้าจอนั้น ให้ลองดูมุมซ้ายล่างของ browser ว่ามีสัญลักษณ์ "!" หรือผิดปกติอะไรอยู่บ้างหรือเปล่า อาจจะมีอะไรแปลกๆแสดงอยู่ก็ได้ ทั้งตอนโหลด หรือตอนกดปุ่ม addCard ไปแล้ว ซึงสามารถดับเบิ้ลคลิกขึ้นมาดู error ได้ หรือใช้คุณสมบัติการ debug ของ browser แต่ละตัวก็ได้ครับ
และถ้าไม่มีอะไรเกิดขึ้นเลยอย่างที่บอกจริงๆ ก็ให้ใช้การ echo (กรณี php) หรือ alert (กรณี javascript) ในส่วนต่างๆของ code มันต้องเจอเข้าสักบันทัดล่ะน่า... นะครับ...
ทักษะการ debug นั้นสำคัญมากต่อกการเขียนโปรแกรมนะครับ!
|
|
|
|
|
Date :
2012-01-01 20:02:59 |
By :
Songkram |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|