Возврат нескольких значений из AJAX с помощью Codeigniter и отображение их в 2 разных областях

Я разрабатываю корзину для покупок с codeigniter. Я использую ajax для удаления элементов корзины.
Моя проблема в том, что я получаю 2 разных ответа на запрос ajax, и я получаю его отлично, но мне нужно отобразить 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
}

Мне нужно отобразить эти ответы в 2 разных областях
echo count($this->cart->contents());
$this->view_shoppingcart();

1

Решение

в вашем контроллере:

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

в вашем коде 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);

}
});

}
2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector