JavaScript不更新表单操作

时间:2015-11-25 00:10:21

标签: javascript php

我没有附加CSS因为这个问题无关紧要但是由于某种原因我的表单不会提交我知道你必须指定一个动作这样做我在javascript中做但每当我点击在按钮上添加到购物车,它只需重新加载此页面Products。已经尝试过显而易见的把addtoCart.php放在动作中,但这会导致其他错误不会像我需要的那样带来产品ID。那么有没有理由javascript没有更新我的表单操作?

<body class="oneColFixCtrHdr">

<div id="container">
<form name="myform" id="myform" action="" method="post"/>
  <input type="hidden" name="PHPSESSID" value="<?php echo session_id; ?>">
  <input type="hidden" name="cartNumber" value="<?php echo $cartNumber ?>">
  <input type="hidden" name="productID" id="productID" value="">    
  <div id="header">
   <table width="760">
   <tr>
   <td width="188" rowspan="2">
    <a href="default.php"> <img  src="images/CongaMoe.jpg" width="150" height="153" alt="Conga Moe Logo" border="0"/></a>
    </td>
    <td width="361"><img src="images/homeTitle.jpg" width="343" height="152"></td>
    <td width="188">
     <a href="default.php"> <img  src="images/CongaMoe.jpg" width="150" height="153" alt="Conga Moe Logo" border="0"></a>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="right">


    <a href="drums.php"><img src="images/btnCongaDrums.jpg" width="100" height="32" border="0"></a>&nbsp;&nbsp;&nbsp;<a href="products.php"><img src="images/btnBuyAConga.jpg" width="100" height="32" border="0"></a>&nbsp;&nbsp;&nbsp;<a href="viewcart.php"><img src="images/btnLookInCart.jpg" width="100" height="32" border="0"></a>&nbsp;&nbsp;&nbsp;<a href="checkout.php"><img src="images/btnPayAndGo.jpg" width="100" height="32" border="0"></a> 
    </td><td width="3"></td>
  </table>
  </div>

  <!-- end #header -->

<div id="mainContent">
<script type="text/javascript">
  function addToCart(product, formObj) {
      document.getElementById("productID").value = product;
      document.getElementById("myform").action = "addtoCart.php";
      formObj.submit;
  }
  </script>
<table>
<?php
require_once('appVars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
   or die('Could not connect to MySQL server as CIS55Student, cis55 database.');
 $strQuote = chr(34);
  $query = "SELECT * FROM products_nalanirowe";
   $rs = mysqli_query($dbc, $query);
   $num_rows = mysqli_num_rows($rs);
   for ($i = 0; $i < $num_rows; $i++) {
      $row = mysqli_fetch_array($rs);
      $productID = $row['productID'];
      $productName = $row['productName'];
      $productPrice = $row['productPrice'];
      echo "<tr class='productText'>";
      echo "<td valign='top'><input type='image' name='submit' src='images/btnAddToCart.jpg' width='100' height='32' border='0' onclick'javascript:addToCart(". $strQuote . "$productID" . $strQuote . ",this.form)' ></td>"; 
      echo "<td valign='top'>". $row['productID'] . "</td>";
      echo "<td valign='top'>". $row['productName'] . "</td>";
      echo "<td valign='top'". $row['productDesc'] . "</td>";
      echo "<td valign='top'>". $row['productPrice'] . "</td>";
      echo "<td valign='top'><img src='" . IMAGEPATH . $row['productImgName'] . "' border='0'></td>";
      echo "</tr>"; 
   }



  mysqli_close($dbc);

?>

</table>
<!-- end #mainContent --></div>
  <div id="footer">
    <p><strong>&copy;Conga Moes 2010</strong></p>
  <!-- end #footer --></div>
  </form>

<!-- end #container --></div>


</body>

2 个答案:

答案 0 :(得分:3)

onclick'javascript:addToCart(". $strQuote . "$productID" . $strQuote . ",this.form)',您没有正确分配处理程序,此formObj.submit;应为formObj.submit();

答案 1 :(得分:2)

另一个错误是

onclick'javascript:addToCart(". $strQuote . "$productID" . $strQuote . ",this.form)' ,

应该阅读

onclick='javascript:addToCart(". $strQuote . "$productID" . $strQuote . ",this.form)' ,