AJAX problems with jquery request sent only some of the time
I have have a website where there are multiple products, the user can add one to their cart and get on screen feedback via ajax that the basket updated.
However on certain products this does not work below is the code that gets used.
THE 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);
}
THE HTML FEEDBACK
<?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">£<?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>
** THE 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;
});
A SUCCESSFUL开发者_如何学JAVA POST
selDerivative-1 171
selQuantity 1
submitted 1
** AN UNSUCCESSFUL POST **
selDerivative-1 223
selQuantity 1
selURL-1 colonial/dining/prestige-dining-for-six
submitted 1
The frmProducts form
<?php echo form_open(current_url(), array('id' => 'frmProducts'), array('submitted' => '1')); ?>
<div class="formRow">
<label for="rattanType"><?php echo $product_attribute_names; ?> </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; ?> - £<?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(); ?>
I have absolutly why the first post would get added to the basket and the second would not, does any one have any idea from looking at my code?
This is kind of a longshot, but I have the suspicion that you have a session problem. The CI Session manager has a few issues randomly creating new sessions from Ajax calls. The issue and some solutions are discussed here:
http://codeigniter.com/forums/viewthread/186070/
精彩评论