javascript - ajax ridirecting when user is not logged in not working
Get the solution ↓↓↓hi guys i have an ecommerce and im using ajax for not loading the page when add to cart is clicked but its not working, when its not logged in products are not adding in to cart but the user is not getting redirect but when its loggedin the add to cart is working well. my data.php, the url in ajax:
<?php
session_start();
require('dbcon.php');
if( isset($_SESSION['nome']) && !empty($_SESSION['nome']) )
{
// if(isset($_POST['prodotti-log'])){
?>
<?php
$pid=$_POST['hidden_id'];
$hnome=$_POST['hnome'];
$prezzo=$_POST['hprezzo'];
$cid=$_SESSION['cid'];
$pqty=$_POST['qty'];
$_SESSION['PID'] = $pid;
$sel="SELECT * FROM cart WHERE pid='$pid' AND cid='$cid'";
$rs=$conn->query($sel);
if ($rs->num_rows>0) {
while ($row=$rs->fetch_assoc()) {
$fqty=$row['qty']+$pqty;
$cart_id=$row['id'];
$upd="UPDATE cart SET qty='$fqty' WHERE id='$cart_id'";
$conn->query($upd);
}
}
else {
$ins = "INSERT INTO cart SET cid='$cid', qty='$pqty', pid='$pid', prezzo='$prezzo',pnome='$hnome'";
$conn->query($ins);
}
// }
// User is logged in, show logout menu here
?>
<?php
}
else
{
// User is not logged in, show login menu here
// if(isset($_POST['prodotti-log']))
// {
header('Location: login.php');
// }
} ?>
what ajax is doing
<script>
$(document).ready(function() {
// Bind the forms submit event
$('.form-prodotti').on('submit', function (event) {
event.preventDefault();
// Here we can use $(this) to reference the correct form
$.ajax({
method: "POST",
url: "data.php",
data: $(this).serialize(),
success: function (response) {
// alert("success");
}
});
});
});
</script>
my products form
if(isset($_GET['id']) && $_GET['id']!=''){
$cat_id=mysqli_real_escape_string($conn,$_GET['id']);
if ($cat_id>0) {
$get_product=get_product($conn,'',$cat_id);
}
else {
?>
<script>
window.location.href = 'index.php';
</script>
<?php
}
}else {
?>
<script>
window.location.href='index.php';
</script>
<?php
}
<?php
if(count($get_product)>0){
foreach($get_product as $list){
?>
<!-- alimentari
i im also using ridirections in my header:
{-code-4}
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: fastcgi sent in stderr: "primary script unknown" while reading response header from upstream
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.