Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I update more than one area on a page with one jquery ajax call?
    primarykey
    data
    text
    <p>I am currently implementing a shopping cart using jQuery, Ajax and PHP and most of it is working. However I am currently stuck on how to update the main shopping basket page content when I update the cart as at the moment my returned html updates the shopping cart area in the sidebar (that works fine).</p> <p>So my question is how do I also update the table that displays all the shopping cart items in the main content area of the site at the same time?</p> <p>My jQuery (for updating the cart):</p> <pre><code>$('.update_cart').submit(function (e) { e.preventDefault(); $.ajax({ cache: false, url: 'inc/shopping_bag.php', data: $(this).serialize() + '&amp;action=update', type: 'POST', success: function(html) { $('#shopping-bag p').html(html); } }); }); </code></pre> <p>My PHP for updating:</p> <pre><code>elseif (!empty($action) &amp;&amp; $action == 'update') { if (is_array($_POST['item_qty'])) { foreach ($_POST['item_qty'] as $key=&gt;$quantity) { $quantity=ceil($quantity); if ($quantity==0) { unset($_SESSION['basket'][$key]); } else { $_SESSION['basket'][$key]['item_qty']=$quantity; } } } update_basket(); } function update_basket() { foreach ($_SESSION['basket'] as $array) { $totalItems+=$array['item_qty']; $cost=$array['item_qty']*$array['unit_price']; $total+=$cost; } echo 'Shopping Bag&lt;br&gt;'.$totalItems.' Items&lt;br /&gt;&amp;pound;'.sprintf("%01.2f", $total); } </code></pre> <p>My shopping cart page (sorry a bit long):</p> <pre><code>&lt;div id="content"&gt; &lt;section&gt; &lt;div class="content-seperator"&gt; &lt;h1&gt;Shopping Bag&lt;/h1&gt; &lt;/div&gt; &lt;div id="inner-content"&gt; &lt;section&gt; &lt;?php if (count($_SESSION['basket']) &gt; 0) { ?&gt; &lt;form action="#" class="update_cart"&gt; &lt;table id="cart"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th width="60"&gt;Quantity&lt;/th&gt; &lt;th&gt;Item&lt;/th&gt; &lt;th width="70"&gt;Unit Price&lt;/th&gt; &lt;th width="60"&gt;Total&lt;/th&gt; &lt;th class="blank" width="90"&gt;&amp;nbsp;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php $total=0; $count=0; foreach ($_SESSION['basket'] as $array) { $check_id = substr($array['item_id'], 0, 5); $cost=sprintf("%01.2f", $array['item_qty']*$array['unit_price']); $total+=$cost; if (!empty($array['image'])) { if (file_exists($array['image'])) { $image_path = $array['image']; } else { $image_path = '&lt;img src="images/missing.gif" alt="missing image" /&gt;'; } } else { $image_path = '&lt;img src="images/missing.gif" alt="missing image" /&gt;'; } $image = getImagesize($image_path); $dimensions = imgResize($image[0], $image[1], 80); if ($check_id=='parts') { ?&gt; &lt;tr&gt; &lt;td class="align-centre"&gt; &lt;input name="item_qty[&lt;?php echo $count; ?&gt;]" type="text" size="1" value="&lt;?php echo $array['item_qty']; ?&gt;" class="formfield align-centre" /&gt; &lt;input name="unit_price[&lt;?php echo $count; ?&gt;]" type="hidden" value="&lt;?php echo $array['unit_price']; ?&gt;" /&gt; &lt;/td&gt; &lt;td&gt; &lt;span class="basket_img"&gt;&lt;img src="&lt;?php echo $image_path; ?&gt;" &lt;?php echo $dimensions; ?&gt; alt="Diesel Injector Image" /&gt;&lt;/span&gt; &lt;strong&gt;&lt;?php echo $array['category']; ?&gt; Injector&lt;/strong&gt;&lt;br /&gt; &lt;span class="basketlabel"&gt;Part No:&lt;/span&gt; &lt;strong&gt;&lt;?php echo $array['item_name']; ?&gt;&lt;/strong&gt; &lt;/td&gt; &lt;td&gt;&amp;pound;&lt;?php echo str_replace("£ ","",$array['unit_price']); ?&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;&amp;pound;&lt;?php echo $cost; ?&gt;&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="?removeItem=&lt;?php echo $array['item_id']; ?&gt;" class="basket_remove"&gt;Remove Item&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } if ($check_id=='prods') { ?&gt; &lt;tr&gt; &lt;td class="align-centre"&gt; &lt;input name="item_qty[&lt;?php echo $count; ?&gt;]" type="text" size="1" value="&lt;?php echo $array['item_qty']; ?&gt;" class="formfield align-centre" /&gt; &lt;input name="unit_price[&lt;?php echo $count; ?&gt;]" type="hidden" value="&lt;?php echo $array['unit_price']; ?&gt;" /&gt; &lt;/td&gt; &lt;td&gt; &lt;span class="basket_img"&gt;&lt;img src="&lt;?php echo $image_path; ?&gt;" &lt;?php echo $dimensions; ?&gt; alt="Diesel Product Image" /&gt;&lt;/span&gt; &lt;strong&gt;&lt;?php echo $array['category']; ?&gt;&lt;/strong&gt;&lt;br /&gt; Product Code: &lt;strong&gt;&lt;?php echo $array['item_name']; ?&gt;&lt;/strong&gt; &lt;/td&gt; &lt;td&gt;&amp;pound;&lt;?php echo sprintf("%01.2f", $array['unit_price']); ?&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;&amp;pound;&lt;?php echo $cost; ?&gt;&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="?removeItem=&lt;?php echo $array['item_id']; ?&gt;" class="basket_remove"&gt;Remove Item&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } $count++; } ?&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;input type="submit" name="update_cart" value="Update Shopping Bag" class="rounded-buttons" /&gt; &lt;input type="button" name="empty_cart" value="Empty Shopping Bag" class="rounded-buttons" /&gt; &lt;/form&gt; &lt;?php } else { ?&gt; &lt;p class="no_items"&gt;You have no items in your shopping bag&lt;/p&gt; &lt;?php } ?&gt; &lt;/section&gt; &lt;/div&gt; </code></pre> <p>Any help greatly appreciated, thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload