如果未设置SESSION
并且客户单击Add to cart
按钮,我想将浏览器重定向到登录页面。
如果window.location.replace("http://app.test/pages/login.php");
为空,我已经编写了此代码customer_id
用于重定向到登录页面。但它不会重定向到此页面。
<script>
$(document).ready(function() {
CountProducts();
$('.add_cart').on('click', function(e) {
e.preventDefault();
var $container = $(this).closest('.col-sm-12');
var name = $container.find('.name').val();
var hiddenID = $container.find('.hiddenID').val();
var price = $container.find('.price').val();
var category = $container.find('.category').val();
var customer_id = <?php echo $_SESSION['customer_id']?>;
if(customer_id === '')
{
window.location.replace("http://app.test/pages/login.php");
}
else{
$.ajax({
url: "Ajax/add_to_cart.php",
type: "post",
data: {
name: name,
hiddenID: hiddenID,
price: price,
category: category
},
success: function(output) {
CountProducts();
}
});
}
});
});
</script>
答案 0 :(得分:2)
尝试以下代码:
if( customer_id > 0 ) {
$.ajax({
url: "Ajax/add_to_cart.php",
type: "POST",
data: {
name: name,
hiddenID: hiddenID,
price: price,
category: category
},
success: function(output) {
CountProducts();
}
});
}
else {
window.location.href = "http://app.test/pages/login.php";
}
答案 1 :(得分:0)
好的,谢谢您的帮助。我忘了将此代码var customer_id = <?php echo $_SESSION['customer_id']?>;
放在引号中。现在我像这样写var customer_id = '<?php echo $_SESSION['customer_id']?>';
,它可以工作。