Reputation: 59
I want to redirect the browser to the login page if SESSION
is not set and a customer clicks on Add to cart
button.
I Have written this code window.location.replace("http://app.test/pages/login.php");
for redirecting to the login page if the customer_id
is empty. but it doesn't redirect to this page.
<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>
Upvotes: 0
Views: 852
Reputation: 59
Ok, Thanks for Helping. I forgot to put this code var customer_id = <?php echo $_SESSION['customer_id']?>;
in quotes Now i write it like this var customer_id = '<?php echo $_SESSION['customer_id']?>';
and it works.
Upvotes: 0
Reputation: 171
Try below code:
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";
}
Upvotes: 2