简体   繁体   中英

Cart is updated only once using Jquery and Codeigniter

I am working on Updating Codeigntier Cart with Jquery using Ajax call to update. Here is my jquery function

$(function() {

    $('.cart_form select').on('change', function(ev) {

        var rowid = $(this).attr('class');
        var qty = $(this).val();

        var postData_updatecart = {
            'rowid' : rowid,
            'qty' : qty

        };

        //posting data to update cart

        $.ajax({

            url : base_url + 'bookings/update_booking_cart',
            type : 'post',
            data : postData_updatecart,

            beforeSend : function() {
                $('#cart_content').html('Updating...');

            },

            success : function(html) {
                //window.location.href = "postproperty.php?type="+suffix+'&page=page1';

                $('#cart_content').html(html);

            }
        });

    });

});

I have cart view file as

<div class="cart_form">
<?php echo form_open(base_url().'index.php/bookings/customerdetails', array('class' => 'bookings_form_customer', 'id' => 'bookings_form_customer')); ?>

<table cellpadding="6" cellspacing="1" style="width:100%" border="0">

<tr>
  <th style="text-align:left">Item Description</th>
  <th style="text-align:left">QTY</th>
  <th style="text-align:right">Item Price</th>
  <th style="text-align:right">Sub-Total</th>
</tr>

<?php $i = 1; ?>

<?php foreach ($this->cart->contents() as $items): ?>

    <?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>

    <tr>
      <td>

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

      </td>
      <td>



            <select name="<?php echo $i.'[qty]'; ?>" class="<?php echo $items['rowid']; ?>">

            <?php
            for($tt=0; $tt<=10; $tt++)
            {
            ?>


            <option value="<?php echo $tt; ?>" <?php if($tt==$items['qty']) echo 'selected="selected"'; ?>><?php echo $tt; ?></option>

            <?php   
            }
            ?>
        </select>

      </td>



      <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
      <td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
    </tr>

<?php $i++; ?>

<?php endforeach; ?>




<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>Total</strong></td>
  <td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>

</table>

<p><?php echo form_submit('update_cart', 'Update your Cart'); ?></p>

</form>
</div>

Things are working Nice. But, lets say if I have 2 products, if i select one item's qty from 1 to 2, cart is updated and updated cart is shown using AJAX. But, if I immediately change another item's qty then it doesnt work.

The cart view file is inside class 'cart_form'.

Helping hands are appreciated.

I got an answer. You can refer below link Ajax Request works only once

OR

Directly follow this

REPLACE jquery code part by

$(function() {   
    $(document).on( "change", "#bookings_form_customer select", function(ev) {
        // your code goes here
    }); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM