Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax Request works only once
    primarykey
    data
    text
    <p>I am working on cart class of codeigniter. I want to update cart when user changes qty of item.</p> <p>I have jquery file like this</p> <pre><code>$(function() { $('#bookings_form_customer select').on('change', function(ev) { var rowid = $(this).attr('class'); var qty = $(this).val(); var postData_updatecart = { rowid : rowid, qty : qty }; //posting data to update cart $.ajax({ url : base_url + 'bookings/update_booking_cart', cache : false, type : 'post', data : postData_updatecart, beforeSend : function() { $('#cart_content').html('Updating...'); }, success : function(html) { $('#cart_content').html(html); } }); }); }); </code></pre> <p>I am requesting to controller below to update the cart.</p> <pre><code>class Bookings extends NQF_Controller { public function update_booking_cart() { $rowid = $this -&gt; input -&gt; post('rowid'); $qty = $this -&gt; input -&gt; post('qty'); $data = array('rowid' =&gt; $rowid, 'qty' =&gt; $qty); $this -&gt; cart -&gt; update($data); $this -&gt; load -&gt; view('shop/cart'); } } </code></pre> <p>My Shop/cart file looks like this</p> <pre><code>&lt;div class="cart_form"&gt; &lt;?php echo form_open(base_url().'index.php/bookings/customerdetails', array('class' =&gt; 'bookings_form_customer', 'id' =&gt; 'bookings_form_customer')); ?&gt; &lt;table cellpadding="6" cellspacing="1" style="width:100%" border="0"&gt; &lt;tr&gt; &lt;th style="text-align:left"&gt;Item Description&lt;/th&gt; &lt;th style="text-align:left"&gt;QTY&lt;/th&gt; &lt;th style="text-align:right"&gt;Item Price&lt;/th&gt; &lt;th style="text-align:right"&gt;Sub-Total&lt;/th&gt; &lt;/tr&gt; &lt;?php $i = 1; ?&gt; &lt;?php foreach ($this-&gt;cart-&gt;contents() as $items): ?&gt; &lt;?php echo form_hidden($i.'[rowid]', $items['rowid']); ?&gt; &lt;tr&gt; &lt;td&gt; &lt;?php echo $items['name']; ?&gt; &lt;?php if ($this-&gt;cart-&gt;has_options($items['rowid']) == TRUE): ?&gt; &lt;p&gt; &lt;?php foreach ($this-&gt;cart-&gt;product_options($items['rowid']) as $option_name =&gt; $option_value): ?&gt; &lt;strong&gt;&lt;?php echo $option_name; ?&gt;:&lt;/strong&gt; &lt;?php echo $option_value; ?&gt;&lt;br /&gt; &lt;?php endforeach; ?&gt; &lt;/p&gt; &lt;?php endif; ?&gt; &lt;/td&gt; &lt;td&gt; &lt;select name="&lt;?php echo $i.'[qty]'; ?&gt;" class="&lt;?php echo $items['rowid']; ?&gt;"&gt; &lt;?php for($tt=0; $tt&lt;=10; $tt++) { ?&gt; &lt;option value="&lt;?php echo $tt; ?&gt;" &lt;?php if($tt==$items['qty']) echo 'selected="selected"'; ?&gt;&gt;&lt;?php echo $tt; ?&gt;&lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;td style="text-align:right"&gt;&lt;?php echo $this-&gt;cart-&gt;format_number($items['price']); ?&gt;&lt;/td&gt; &lt;td style="text-align:right"&gt;$&lt;?php echo $this-&gt;cart-&gt;format_number($items['subtotal']); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php $i++; ?&gt; &lt;?php endforeach; ?&gt; &lt;?php if($this -&gt; session -&gt; userdata('booking_pickup')=='courier') { ?&gt; &lt;tr&gt; &lt;td&gt;Pick Up Fees&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td style="text-align:right"&gt;HK $20&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;?php if($this -&gt; session -&gt; userdata('booking_return')=='courier') { ?&gt; &lt;tr&gt; &lt;td&gt;Drop Off Fees&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td style="text-align:right"&gt;HK $20&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;tr&gt; &lt;td colspan="2"&gt; &lt;/td&gt; &lt;td class="right"&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt; &lt;td class="right"&gt;$&lt;?php echo $this-&gt;cart-&gt;format_number($this-&gt;cart-&gt;total()); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;p&gt;&lt;?php echo form_submit('update_cart', 'Update your Cart'); ?&gt;&lt;/p&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>When I changed select option (that contains qty of product), The AJAX request is sent and it works. But, It works only for the first time, it doesnt work from second time onwards. </p> <p>When I delete ajax request call and place alert instead, it works everytime. That means there is problem on ajax part only. I am stuck since day before yesterday. Please help.</p>
    singulars
    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.
    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