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

时间:2017-09-11 06:19:28

标签: php ajax codeigniter shopping-cart

我正在开发一个带有codeigniter的购物车。我正在使用ajax删除购物车物品。 我的问题是我在ajax请求上返回2个不同的响应,我得到它完美但我需要在不同的地方显示2个不同的HTML响应

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

     }
  });

}  

查看页面

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

控制器

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
}

我需要在两个不同的区域显示这些响应 echo count($this->cart->contents()); $this->view_shoppingcart();

1 个答案:

答案 0 :(得分:2)

在您的控制器中:

file.xlsx
你的ajax代码中的

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