Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I update my cart with Ajax?
    text
    copied!<p>I would really need some help from to AJAX Guru master overthere to help me building my update cart function on my website in AJAX.</p> <p>So basically, what I would like to do is, when I modify one of my product in one input_dropdown, my 'update_cart' function is automaticaly called and my prices are updated as well as my input</p> <p>EDIT : I rewrite my question since I made some progress thanks to Matei</p> <p>Here is my view :</p> <pre><code>&lt;?php $options = array( '0' =&gt; '0', '1' =&gt; '1', '2' =&gt; '2', '3' =&gt; '3', '4' =&gt; '4', '5' =&gt; '5', '6' =&gt; '6', '7' =&gt; '7', ); if($product['quantity']==0){ $value[$product['title']] = set_value('quantity'.$product['title']); }else{ $value[$product['title']] = $product['quantity']; } $data0 = 'class="quantSelect" value="'.$value[$product['title']].'" id="quant'.$product['title'].'"'; echo form_dropdown('quantity'.$product['title'], $options, $value[$product['title']],$data0); ?&amp;gt; &lt;/td&gt; &lt;td&gt; &amp;lt;?php echo $product['price'] ?&amp;gt; &lt;/td&gt; &lt;td id="&amp;lt;?php echo 'price'.$product['title']?&amp;gt;"&gt; $&amp;lt;?php echo $total[$product['title']] ?&amp;gt; &lt;/td&gt;[/code] </code></pre> <p>Well, everything is in a foreach loop but I think that here, it doesn't matter.</p> <p>Then I tried to set up the Matei AJAX function :</p> <pre><code>$(".quantSelect").click(function(){ $.POST("&amp;lt;?php echo base_url().'main/update_cart';?&amp;gt;", {product_id:$('&amp;lt;?php echo $product['quantity']; ?&amp;gt;').val(),quantity:$('&amp;lt;?php echo 'quantity'.$product['title'] ?&amp;gt;').val()}, function(data){ if(data.success) { $("&amp;lt;?php echo 'price'.$product['title']?&amp;gt;").val(data.some_returned_value); // update value of an text input or textarea (view more info about jQuery selectors) $("#totalPriceWithTaxes").html(data.some_other_returned_value); // update value of a paragraph } }, 'json'); }); </code></pre> <p>And at last the update cart function : </p> <pre><code>function update_cart(){ $success = false; if(!empty($_POST['product_id']) &amp;&amp; !empty($_POST['quantity']) &amp;&amp; is_numeric($_POST['quantity'])) { // I get all the information i need here in order to calcul the final price //We calcul the final price with taxes, shipping and everything. $data['totalPriceWithTaxes'] = $data['tax'] + $data['totalPrice'] + $data['Shipping']-&gt;shipping; $this-&gt;session-&gt;set_userdata('totalPriceWithTaxes', $data ['totalPriceWithTaxes']); $success = true; $some_returned_value = 69; $some_other_returned_value = $data['totalPriceWithTaxes']; // the final price } echo json_encode(array("success" =&gt; $success, "some_returned_value" =&gt; $some_returned_value, "some_other_returned_value" =&gt; $some_other_returned_value)); } </code></pre> <p>Here we are, so I can't see any update. If someone could help me to figure out how to set up that AJAX Function, I would deeply appreciate :)</p>
 

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