【问题标题】:AJAX problems with jquery request sent only some of the time仅在某些时候发送的 jquery 请求的 AJAX 问题
【发布时间】:2011-04-06 15:41:52
【问题描述】:

我有一个网站,其中有多种产品,用户可以将其中一种添加到他们的购物车中,并通过 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 脚本**

$("#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(); ?>

我绝对知道为什么第一个帖子会被添加到购物篮中而第二个不会,有没有人通过查看我的代码有任何想法?

【问题讨论】:

  • 我们可以看看frmProducts 表格吗?
  • 表单已添加到帖子中

标签: php ajax codeigniter jquery


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 2019-03-31
    • 2017-01-14
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多