php - Returning multiple values from AJAX with Codeigniter and display them in 2 different areas -


i developing shopping cart codeigniter. using ajax remove cart items. issue returnig 2 different response on ajax request , getting need display 2 different html response in different places

ajax code

function remove_cart(itemid) {         $.ajax({             type: 'post',             url: '<?php echo site_url("ajax_controller1/remove_cart/'+itemid+'")?>',             data: { id:itemid },              success:function(response){              $("#shoppingcart_container").html(response);       }   });  }   

view page

<td><a onclick="remove_cart('<?=$items['rowid'];?>')" class="icon-close" ><span aria-hidden="true" class="icon_close"></span></a></td> 

controller

public function remove_cart($id)     {             $data = array(                'rowid' => $id,                'qty'   => 0             );          $this->cart->update($data);                 echo count($this->cart->contents());         $this->view_shoppingcart();     } public function view_shoppingcart(){ ?>         <table class="table table-striped shopping-cart-table">                   <thead class="font-poppins">                     <tr>                     <th></th>                     <th>photo</th>                     <th>product</th>                     <th>unit price</th>                     <th>quantity</th>                     <th>total</th>                     <th></th>                     </tr>                   </thead>                   <tbody> <?php $n = 0; foreach ($this->cart->contents() $items){  $query = $this->db->get_where('products', array('product_id' => $items['id'])); $val=$query->result_array(); $n++; ?>                     <tr>                       <td class="text-center"><?php echo $n; ?></td>                       <td><a href="#"><img src="<?php echo base_url();?>assets/images/products/<?php echo $val[0]['img_name'];?>" alt="img" class="popular"></a></td>                       <td><a href="#" class="font-poppins"> <?php echo $items['name']; ?>              <?php if ($this->cart->has_options($items['rowid']) == true): ?>                  <p>                     <?php foreach ($this->cart->product_options($items['rowid']) $option_name => $option_value): ?>                          <strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />                      <?php endforeach; ?>                 </p>              <?php endif; ?> </a></td>                       <td><div class="font-black">inr <?php echo $this->cart->format_number($items['price']); ?></div></td>                       <td>                         <form class="form">                           <input type="number" class="input-border white-bg" style="width: 60px;" min="1" max="100" value="<?php echo $this->cart->format_number($items['qty']); ?>">                         </form>                       </td>                       <td><div class="font-black">inr <?php echo $this->cart->format_number($items['subtotal']); ?></div></td>                       <td><a onclick="remove_cart('<?=$items['rowid'];?>')" class="icon-close" ><span aria-hidden="true" class="icon_close"></span></a></td>                     </tr>      <?php } ?>                                     </tbody>                   </table> <?php } 

i need display these response in 2 different areas echo count($this->cart->contents()); $this->view_shoppingcart();

in controller:

public function removecart() { .. $data  = array( 'count'=>$this->cart->contents(),//modify return count. 'cart'=> $this->view_shoppingcart()//modify return html output. );  echo json_encode($data); } 

in ajax code:

function remove_cart(itemid) {         $.ajax({             type: 'post',             url: '<?php echo site_url("ajax_controller1/remove_cart/'+itemid+'")?>',             data: { id:itemid },              success:function(response){              response= json.parse(response);              $("#shoppingcart_container").html(response.cart);              $("#count_container").html(response.count);       }   });  }   

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -