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 trial

JavaScript

a little problem of changing input product number

i have a page of cart that cannot change total amount when i input the quantity

<?php 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(); // ????

//---------------------- ????? --------------------- // ????????????? 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("?????????");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(\"?????????\");" . "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">???????</th></tr> <tr style="background-color:silver;color:white"> <td height="23" width="80">?? <!----- ???????????? checkbox -----> <input type="checkbox" name="all" onClick="select_all('form1','p_id[]',this.name);"> </td> <td height="23" width="460">????</td> <td height="23" width="60">??</td> <td width="10" height="23"></td> <td height="23" width="60">??</td> <td width="10" height="23"></td> <td width="70" height="23">??</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">???</td> <td align="right" style="border-top:1px solid silver;width:70"> <input type="text" name="total" value="<?php echo getTotal(); // ?????? ?>" id="total"> </td> </tr> </table>

<!-- -------------------- ?????? -------------------- --> <table width="800" border="0" align="center"> <tr> <td height="23" align="center"> <input type="button" name="DEL" value="????" onClick="settozero('form1','p_id[]')"> </td> <td height="23" align="center"> <input type="button" name="UPD" value="????" onClick="<?php $chaObj->printscript(); ?>"> </td> <td height="23" align="right"> <input type="button" name="CONT" value="????" onClick="window.close();"> </td> <td height="23" align="right"> <input type="button" name="CONT" value="????" onClick="location.href='checkout.php';"> </td> </tr> </table> </form>

1 Answer

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I haven't worked with this cart plugin before, so I can't recognize the issue.

By the way, you can get a block of code formatted nicely here in the forum if you put three backticks (`) on the line above it and three backticks on the line below it. If you type something like this ...

```
<?php echo "Hello, world!"; ?>
```

... then it will be formatted like this:

<?php echo "Hello, world!"; ?>