我正在通过ajax向控制器发布id。但它不起作用,ajax函数会给大数据一些类似于table的响应。请查看代码并指导我哪里错了。
HTML
<li class="sendRequest">
<a href="javascript:void(0);" onclick="cart.add('<?php echo $product['product_id']; ?>');"> <i class="fa fa-shopping-cart"></i><!--<img src="images/cart-icon.png" alt="cart" />--></a>
<div>Add To Container</div>
<form id="formSend" method="post" >
<input type="hidden" value="<?php echo $product['product_id']; ?>" name="sendProductId" class="pID" />
</form>
</li>
JS
$( ".sendRequest" ).each(function(index) {
$(this).on("click", function(){
var otherInput = $(this).find('.pID').val();
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:otherInput,
success:function(response){
alert(response);
}
});
});
});
Php代码
if(isset($this->request->get['sendProductId'])){
$cookie_value = $this->request->get['sendProductId'];
setcookie($cookie_name, $cookie_value, time() + (86400 * 30));
}
print_r($_COOKIE);
此外,当我打印cookie的全局变量时,它也给出了空白数组。
替换
data:otherInput,
带有
data:{'sendProductId':otherInput},
如下更正Ajax调用。
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:{'sendProductId':otherInput},
success:function(response){
alert(response);
}
});