使用Codeigniter从AJAX返回多个值,并在两个不同的区域显示它们

时间:2022-06-16 09:51:05

I am developing a shopping cart with codeigniter. I am using ajax to remove cart items. My issue is I am returnig 2 different response on ajax request and i am getting it perfectly but i need to display the 2 different html response in different places

我正在用codeigniter开发购物车。我正在使用ajax删除cart项目。我的问题是,我在ajax请求上恢复了2种不同的响应,而且我得到了完美的响应,但是我需要在不同的地方显示2种不同的html响应

Ajax Code

Ajax代码

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() 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
}

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

我需要在2个不同的区域显示这些响应,回波计数($this->cart->content ()));$ this - > view_shoppingcart();

1 个解决方案

#1


2  

in your controller:

在你的控制器:

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 your ajax code:

在ajax代码:

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

     }
  });

}  

#1


2  

in your controller:

在你的控制器:

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 your ajax code:

在ajax代码:

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

     }
  });

}