2017-09-11 1 views
1

Ich entwickle einen Warenkorb mit Codeigniter. Ich benutze Ajax, um Einkaufswagen zu entfernen. Mein Problem ist, ich bin returnig 2 unterschiedliche Antwort auf Ajax-Request und ich erhalte es perfekt, aber ich braucheMehrere Werte aus AJAX mit Codeigniter zurücksenden und in 2 verschiedenen Bereichen anzeigen

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); 

    } 
    }); 

} 

Ansicht Seite die 2 verschiedene HTML-Antwort an verschiedenen Stellen angezeigt werden

<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() as $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']) as $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 
} 

Ich brauche diese Reaktion in 2 verschiedenen Bereichen echo count($this->cart->contents()); $this->view_shoppingcart();

Antwort

1

in Ihrem Controller angezeigt werden:

public function removeCart() 
{ 
.. 
$data = array(
'count'=>$this->cart->contents(),//modify this to return count. 
'cart'=> $this->view_shoppingcart()//modify this to return the html output. 
); 

echo json_encode($data); 
} 

in Ihren 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); 

    } 
    }); 

} 
+0

Ich erhalte Fehler 'Parse Fehler: Syntaxfehler, unerwarteter '' Einkaufswagen '' (T_CONSTANT_ENCAPSED_STRING), erwartet ')' in ' –

+0

gemacht und bearbeitet , Hör zu. – Kalu

+0

Vielen Dank für Ihre Antwort, aber leider funktioniert es nicht. wenn ich alert alarm (response'' Ich bekomme Ergebnisse, aber wenn alert (response.cart) 'oder alert (response.count)' es zeigt undefined –

Verwandte Themen