dsjbest2014 2016-05-06 23:11
浏览 41
已采纳

Ajax调用提交的时候不应该?

Ok so I am making an Ecommerce website and up until recently the update_cart (quantity) code has been working fine. Now suddenly it has decided to submit my form instead of running the update. I have tried changing a lot as well as moving it outside the form in hopes it would fix the issue. I am sorry for my more than likely sloppy code. If you have any ideas or suggestions please let me know. I know my php, sql, and html pretty well but I know very little concerning ajax and jquery.

I have spent the last 6 hours beating my head over this so any and all help is greatly appreciated!

My cart

  <form class="form" action="cart.php?checkout=1" method="post">
      <table  id="totals" class="table table-bordered table-condensed  table-striped table-auto">
        <thead>
          <th>#</th>
          <th>Item</th>
          <th>Price</th>
          <th>Quantity</th>
          <th>Sub Total</th>
        </thead>
        <tbody>
          <?php

            foreach ($items as $item) {
              $product_id = $item['id'];
              $productQ = $db->query("SELECT * FROM products WHERE id = '{$product_id}'");
              $product = mysqli_fetch_assoc($productQ);

              ?>

              <tr>
                <td><?=$i;?></td>
                <td><?=$product['title'];?></td>
                <input type="hidden" name="item<?=$i;?>" value="<?=$product['title'];?>">
                <td><?=money($product['price']);?></td>
                <input type="hidden" name="price<?=$i;?>" value="<?=$product['price'];?>">
                <td>
                  <button class="btn btn-xs btn-default" onclick="update_cart('removeone','<?=$product['id'];?>');">-</button>
                  <?=$item['quantity'];?>
                  <?php if($item['quantity'] < $product['quantity']): ?>
                    <button class="btn btn-xs btn-default" onclick="update_cart('addone','<?=$product['id'];?>');">+</button>
                  <?php else: ?>
                    <span class="text-danger">Max</span>
                  <?php endif; ?>
                </td>
                <td ><?=money($item['quantity'] * $product['price']);?></td>
                <input type="hidden" name="quantity<?=$i;?>" value="<?=$item['quantity'];?>">
              </tr>

          <?php
           $i++;
           $item_count += $item['quantity'];
           $grand_total += ($product['price'] * $item['quantity']);
            }

             ?>
        </tbody>
      </table>





      <table id="totals"  class="table table-bordered table-condensed text-right table-auto">
          <thead class="totals-table-header">
            <th></th>
            <th></th>
            <th></th>
          </thead>
          <tbody>
            <tr>
              <td>Total Items:</td>
              <input type="hidden" name="rows" value="<?=$i;?>">
              <td><?=$item_count;?></td>
              <input type="hidden" name="item_count" value="<?=$item_count;?>">
            </tr>
            <tr>
              <td>Total:</td>
              <td class="bg-success"><?=money($grand_total);?></td>
              <input type="hidden" name="total"  value="<?=$grand_total;?>">
            </tr>
            <tr>
              <td></td>
              <td>    <button type="submit"  class="btn btn-primary btn-sm ">
                  <span class="glyphicon glyphicon-shopping-cart"></span>Check Out >>
                  </button></td>
            </tr>


          </tbody>
      </table>

    </div>




</form>

My update ajax (in footer)

function update_cart(mode,edit_id){
  var data = {"mode" : mode, "edit_id" : edit_id};
  jQuery.ajax({
    url : '/admin/parsers/update_cart.php',
    method : "post",
    data : data,
    success : function(){location.reload(true);},
    error : function(){alert("Something went wrong.");},
  });
}

and finally my update_cart function

<?php
  require_once $_SERVER['DOCUMENT_ROOT'].'/system/init.php';
  $mode = sanitize($_POST['mode']);
  $edit_id = sanitize($_POST['edit_id']);
  $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
  $result = mysqli_fetch_assoc($cartQ);
  $items = json_decode($result['items'], true);
  $updated_items = array();
  $domain =(($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false);

  if ($mode == 'removeone') {
    foreach ($items as $item) {
      if ($item['id'] == $edit_id) {
        $item['quantity'] = $item['quantity'] - 1;
      }
      if ($item['quantity'] > 0) {
        $updated_items[] = $item;
      }
    }
  }


  if ($mode == 'addone') {
    foreach ($items as $item) {
      if ($item['id'] == $edit_id) {
        $item['quantity'] = $item['quantity'] + 1;
      }
        $updated_items[] = $item;
    }
  }

if (!empty($updated_items)) {
  $json_updated = json_encode($updated_items);
  $db->query("UPDATE cart SET items = '{$json_updated}' WHERE id = '{$cart_id}'");
  $db->query("UPDATE transactions SET status ='' WHERE cart_id = {$cart_id}");
  $_SESSION['success_flash'] = 'Your shopping cart has been updated!';
}

if (empty($updated_items)) {
  $db->query("DELETE FROM cart WHERE id = '{$cart_id}'");
  setcookie(CART_COOKIE,'',1,"/",$domain,false);
}

 ?>
  • 写回答

1条回答 默认 最新

  • dsfs1233 2016-05-06 23:35
    关注

    button without type will submit the form. Your options are adding type="button" or cancelling the onclick event.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换