jQuery请求的AJAX问题只在某些时候发送

时间:2011-04-06 15:41:53

标签: php ajax codeigniter jquery

我有一个网站,其中有多个产品,用户可以添加一个到他们的购物车,并通过更新篮子的ajax获得屏幕反馈。

但是,在某些产品中,这不起作用的是使用的代码。

PHP

function updateBasket()
    {
        $this->load->model('Checkout_model');
        $this->load->model('Product_model');
        $derivativeId = $this->input->post('selDerivative-1');
        $quantity = $this->input->post('selQuantity');
        $derivative = $this->Product_model->GetProducts(array('pdId' => $derivativeId), 'small');

        // Add item to shopping bag.
        $attributes = $this->Product_model->GetProductDerivatives(array('pdId' => $derivativeId));
        $this->Checkout_model->AddProduct($derivative, $attributes, $quantity);
        $this->data['message'] = 'Item added to Shopping Bag.';

        // Update Delivery Price
        $this->Checkout_model->updateDelivery(49);

        $this->data['items'] = $this->Checkout_model->GetProducts();

        $this->template
        ->build('checkout/quickbasket', $this->data);
    }

HTML反馈

    <?php
    //var_dump($items);
    //print_r($this->session->userdata);
?>

<div id="basketoverview">
<div id="quickbasket">
    <h1><?php echo $this->cart->total_items(); ?> item in bag</h1>
    <?php foreach ($items as $item) : ?>
        <div class="item">
            <img src="<?php echo base_url().$item['imageUrl'];?>" alt="<?php echo $item['imageAlt'];?>" width="70"/>
            <h4><?php echo $item['imageAlt'];?></h4>
            <span class="price">&pound;<?php echo $item["price"]; ?></span>
            <span class="qauntity">Quantity: <?php echo $item['qty']; ?></span>
        </div>
    <?php endforeach; ?>
</div>
<div id="basket_options">
    <a href="/checkout/showbag">VIEW BAG</a> / <a href="/checkout/delivery_and_billing">CHECKOUT</a>
 </div>
 </div>

** AJAX SCRIPT **

$("#frmProducts").submit(function(){
            var dataSet = $("#frmProducts").serialize();
            $.ajax({
              url: "<?php echo base_url();?>products/updateBasket",
              data: dataSet,
              type: "POST",
              success: function(data){
                $('html, body').animate({scrollTop:0}, 'slow');
                $("#miniCart").load("<?php echo base_url();?>checkout/loadCartView");
                $('body').append(data); 
                $('#basketoverview').fadeIn(2000);
                setTimeout(function () { $('#basketoverview').fadeOut(2000).hide(); }, 8000);
              }
            });
            return false;
        });

成功发布

selDerivative-1 171
selQuantity 1
submitted   1

**一次不成功的发布**

selDerivative-1 223 
selQuantity 1
selURL-1    colonial/dining/prestige-dining-for-six
submitted   1

frmProducts表格

<?php echo form_open(current_url(), array('id' => 'frmProducts'), array('submitted' => '1')); ?>

                <div class="formRow">
                    <label for="rattanType"><?php echo $product_attribute_names; ?>&nbsp;</label><br />
                    <?php
                    $options = array();
                    foreach ($product_derivatives as $derivative) :
                        $options[$derivative['derivativeId']] = $derivative['attributeValues'];
                    endforeach;
                    ?>
                    <?php echo form_dropdown('selDerivative-1', $options, $product_details->pdId, 'class="select clear" id="selDerivative-1"'); ?>
                </div>

                <?php if (count($individual_products) > 0) : ?>
                <div class="formRow">
                    <label for="itemType">Item</label><br />
                    <select class="select clear" id="selURL-1" name="selURL-1">
                        <option value="<?php echo current_url(); ?>">Full Set</option>
                    <?php foreach ($individual_products as $product) : ?>
                        <option value="<?php echo site_url($product->fullProductPath); ?>"><?php echo $product->productTitle; ?> - &pound;<?php echo ($product->productSavingType != 'none' ? $product->productSavingPrice : $product->productPrice); ?></option>
                    <?php endforeach; ?>
                    </select>

                    <input id="btnGo-1" name="btnGo-1" type="submit" value="GO" />
                </div>
                <?php endif; ?>

                <div class="formRow">
                    <label for="addQty">Quantity</label><br />
                    <?php
                    $options = array();
                    for ($i = 1; $i < 10; $i++) :
                        $options[$i] = $i;
                    endfor;
                    ?>
                    <?php echo form_dropdown('selQuantity', $options, '1', 'class="small select clear"'); ?>
                </div>

                <input type="submit" value="add to bag" name="btnAddToBag" id="btnAddToBag" />

            <?php echo form_close(); ?>

我绝对清楚为什么第一篇文章会被添加到购物篮中而第二篇文章没有,是否有任何人在查看我的代码时有任何想法?

1 个答案:

答案 0 :(得分:0)

这是一种远景,但我怀疑你有会话问题。 CI会话管理器有几个问题从Ajax调用中随机创建新会话。这里讨论了问题和一些解决方案:

http://codeigniter.com/forums/viewthread/186070/