1

May be its a begginer question but i think i forget something which i cant find because i face such error before where ajax returns html page data instead. but this time I am facing one more issue When i click on button it also return login page below of my current page . Instead of redirecting it login page Open the login page below of current page so without further do here is my code

$(document).on("click","#submit_review",function(e){
e.preventDefault();
var review_comment = $("#review_comment").val();
 var productid = getUrlParameter('id');
    
var reviewarray = [review_comment,productid];
var dataArr = JSON.stringify(reviewarray);    

$.ajax({
        url:"review-ajax.php",
        type:"POST",
        data:{review:dataArr},
        success:function(response){
            alert(response);
            }); 

Here is my ajax code

session_start();
if(isset($_POST['review'])){
    if(isset($_SESSION['email'])){
        
    $json_data = json_decode($_POST['review']);
    $review_comment = htmlspecialchars($json_data[0]);
    $product_id = htmlspecialchars($json_data[1]);        
    $email = $_SESSION['email'];
        
    include "conn.php";    
        
    $useridquery = $con->prepare("SELECT * FROM user WHERE email =:email");    
    $useridquery->bindParam(":email",$email);
    $useridquery->execute();
    $userresult = $useridquery->fetch();   
    $userid = $userresult['id'];
    
    $date = date('d-m-Y');    
        
        
    //Insert in the review table    
    $reviewInsertQuery = $con->prepare("INSERT INTO product_review(product_id, user_id, review_comment,date) VALUES (:product,:user_id,:review,:date)");
    $reviewInsertQuery->bindParam(":product",$product_id);
    $reviewInsertQuery->bindParam(":user_id",$userid);
    $reviewInsertQuery->bindParam(":review",$review_comment);
    $reviewInsertQuery->bindParam(":date",$date);    
    $reviewInsertQuery->execute();        
    
    echo 1; 
        
    }else{
      header('location:loginregister.php'); 
    }
}
   


?>
Azeez
  • 368
  • 2
  • 10
  • The `header()` function will change the target of the ajax call, so whatever happens in `loginregister.php` will be returned to ajax. It does not tell ajax js to redirect the user to `loginregister.php`. – IncredibleHat Jul 24 '20 at 14:55
  • so should i not use header function? – Azeez Jul 24 '20 at 14:57
  • 1
    Correct. Instead return something (json preferably) that you use in the ajax success handler to then redirect the user with `window.location.href = 'wheretogo';`. Heres a random answer about it: https://stackoverflow.com/a/44949236/2960971 -or- https://stackoverflow.com/questions/20975546/php-header-doesnt-redirects-after-ajax-is-called -or- https://stackoverflow.com/questions/18745669/ajax-php-headerlocation – IncredibleHat Jul 24 '20 at 15:00
  • Regarding `htmlspecialchars()`, please read https://stackoverflow.com/a/4882317/2943403 . The user's `id` should be saved in the session, not the email. Please read https://stackoverflow.com/a/42869960/2943403 – mickmackusa Aug 15 '20 at 12:17
  • You should be inserting the current date in `Y-m-d` format to enjoy all of the date type benefits that mysql has to offer. You shouldn't even pass a date string from php -- just use `CURRENT_DATE` https://stackoverflow.com/q/28094069/2943403 – mickmackusa Aug 15 '20 at 12:33
  • You must not manually craft a json string ever. You are creating a variable, stringifying it, then nesting that string inside of a manually produced json-like string (no double quoting around `review` means that you are POSTing invalid json data to the php script -- confirm for yourself @ jsonlint). – mickmackusa Aug 15 '20 at 12:38

1 Answers1

0

dont use header('location:loginregister.php'); else use echo 0; and put a condition on ajax respose where you are alerting the message

Hammad Ahmed khan
  • 1,618
  • 7
  • 19