Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPriscilla Kam
1,086 Pointsbutton of change quantity problem
hi everyone i have a shopcart.php , whenever i change the quantity the product and add the product with new amount , the new quantity seems like not change. Do anyone know the problem?
<?php
error_reporting(0);
session_start();
header('Content-type: text/html; charset=utf-8');
include_once('../../xajax_core/xajax.inc.php'); // ?? xajax
$xajax = new xajax(); // ?? xajax ??
// ???????
$chaObj=$xajax->registerFunction('change_quantity');
$chaObj->useSingleQuote();
$chaObj->addParameter(XAJAX_FORM_VALUES,'form1');
$xajax->processRequest(); // ????
$xajax->printJavascript();
//---------------------- ????? ---------------------
// ?????????????
function change_quantity($form) {
$objResponse = new xajaxResponse(); // ??????
// ???? Checkbox ??? p_id, ????, ??????
// ?????????? 'p_id' ??, ??????????
// ????????
if(isset($form['p_id'])) {
// ?????????????
foreach($form['p_id'] as $p_id) {
// ???????????????
if($form['qua'][$p_id]=='0') {
// ???????? 0, ????????????
unset($_SESSION['cart'][$p_id]);
if(count($_SESSION['cart'])==0) { // ????????
$objResponse-> // ?????
script('alert("cart has no items");window.close();');
unset($_SESSION['cart']);
}
else { // ???????????
// ?????????????
$objResponse->remove($p_id);
// ???????????
$objResponse->assign('total','innerHTML',gettotal());
}
}
else if ($form['qua'][$p_id]>0) {
// ????? 0 ??, ?????????????
$_SESSION['cart'][$p_id]['quantity'] = $form['qua'][$p_id];
// ??????, ?????????????????
$objResponse->assign('sub[' . $p_id . ']', 'innerHTML',
$_SESSION['cart'][$p_id]['quantity'] *
$_SESSION['cart'][$p_id]['price']);
$objResponse->assign('total','innerHTML',gettotal());
}
else {
// ??????, ?????????
$objResponse->assign('qua[' . $pid . ']', 'innerHTML',
$_SESSION[$pid]['quantity']);
}
}
}
return $objResponse; // ??????
} // ???? change_quantity() ??
// ????????
function getTotal() {
$total = 0;
foreach($_SESSION['cart'] as $p_id => $item)
$total += ($item['quantity'] * $item['price']);
return $total;
}
//---------------------- ??? HTML ---------------------
// ?????????, ?????????????????
if( !isset($_SESSION['cart']) ) { // ???????
echo "<script>alert(\"you haven't chosen any products\");" .
"window.close();</script>";
exit();
}
?>
<title>?????</title>
<!-- -------------------- ??? JavaScript ------------------ -->
<script type="text/javascript">
// ?????????????? checkbox ???
function select_all(formName, elementName, selectAllName) {
elem = document.forms[formName].elements[elementName];
if(!elem) // ??????
return;
else if(elem.length!= null) // ????????? (elem ???)
for(var i = 0; i < elem.length; i++)
elem[i].checked =
document.forms[formName].elements[selectAllName].checked;
else
elem.checked =
document.forms[formName].elements[selectAllName].checked;
}
// ?????????????0, ????????
function settozero(formName, elementName) {
elem = document.forms[formName].elements[elementName];
if(!elem) // ??????
return;
else if(elem.length!= null) { // ????????? (elem ???)
for(var i = 0; i < elem.length; i++)
if(elem[i].checked) {
var qua =
document.getElementById("qua[" + elem[i].value + "]");
var subtotal = document.getElementById("sub[" + elem[i].value + "]");
qua.value = 0; // ????? 0
subtotal.value = 0;
}
}
else {
var qua = document.getElementById("qua[" + elem.value + "]");
var subtotal = document.getElementById("sub[" + elem.value + "]");
qua.value = 0; // ????? 0
subtotal.value = 0;
}
var total_value = document.getElementById("total");
total_value.value = 0;
<?php $chaObj->printScript(); // ???????????? ?>
}
</script>
<?php $xajax->printJavaScript('/'); ?>
<!-- ------------------- ???????? ------------------- -->
<link rel="StyleSheet" type="text/css" href="../module.css" />
<form name="form1" id="form1" method="post" action="checkout.php">
<table width="800" border="0" align="center"
cellspacing="0" cellpadding="2"
style="text-align:center;border:1px solid silver">
<tr><th colspan="7">cart</th></tr>
<tr style="background-color:silver;color:white">
<td height="23" width="80">all
<!----- ???????????? checkbox ----->
<input type="checkbox" name="all"
onClick="select_all('form1','p_id[]',this.name);">
</td>
<td height="23" width="460">product name</td>
<td height="23" width="60">unit price</td>
<td width="10" height="23"></td>
<td height="23" width="60">quantity</td>
<td width="10" height="23"></td>
<td width="70" height="23">subtotal</td>
</tr>
<!-- -------------------- ??????? -------------------- -->
<?php
// ?????????
$total = 0;
foreach($_SESSION['cart'] as $p_id => $item) {
// ???? id ???? $p_id, ???????
echo "<tr id='$p_id'>\n";
echo "<td width='80' height='21'>" .
"<input type='checkbox' name='p_id[]' value='" .
$p_id . "'></td>\n";
echo "<td>" . $item['name'] . "</td>\n";
echo "<td width='60' >" . $item['price'] . "</td>\n";
echo "<td width='10' style='border-width:0'>×</td>\n";
echo "<td width='60'>" .
"<input type='text' name='qua[$p_id]' id='qua[$p_id]'
value='" . $item['quantity'] . "' size='3'></td>";
echo "<td width='10' style='border-width:0'>=</td>\n";
echo "<td width='70' align='right'>" ."<input type='text' size='6' id='sub[$p_id]' value='".($item['quantity'] * $item['price'])."' disabled>"."</td></tr>\n";
}
?>
<tr>
<td align="right" colspan="6">total:</td>
<td align="right" style="border-top:1px solid silver;width:70">
<input type="text" name="total" value="<?php echo gettotal(); // display total amount ?>" id="total">
</td>
</tr>
</table>
<!-- -------------------- cart button -------------------- -->
<table width="800" border="0" align="center">
<tr>
<td height="23" align="center">
<input type="button" name="DEL" value="remove products"
onClick="settozero('form1','p_id[]')">
</td>
<td height="23" align="center">
<input type="button" name="UPD" value="change quantity"
onClick="xajax_change_quantity(xajax.getFormValues('form1'))">
</td>
<td height="23" align="right">
<input type="button" name="CONT" value="continue shopping"
onClick="window.close();">
</td>
<td height="23" align="right">
<input type="button" name="CONT" value="checkout"
onClick="location.href='checkout.php';">
</td>
</tr>
</table>
</form>
4 Answers
Nick Pettit
Treehouse TeacherHi Priscilla Kam,
This is quite a lot of code and it's very confusing because most of the comments are just question marks. Do you think you could offer some more clarification?
For example, did you pull this code from somewhere else? If so, is there some documentation that could shed some light on the issue?
Randy Hoyt is our PHP teacher, so he may be able to figure this out a little more quickly than I can. :)
Priscilla Kam
1,086 Pointshi nick i love your tutorial so much^^ , ur video is so nice , keep going , btw, do you have skype so that i can send u my source code about it
Priscilla Kam
1,086 Pointsmay you add me in skype? my skype is benny.cheung@jet-dynamic.com
Nick Pettit
Treehouse TeacherHi Priscilla,
I'd much prefer that you post any source code here. Our Treehouse students are all very smart and will probably be able to help you faster than I can. I'm very busy these days helping build our company and finding new ways to teach as many students as we can!