0

I have two problems really here, the first one being that when I try to evaluate response.status I get undefined and the second one being that my "$_SESSION['promo-code'] = $arr" is also not being created. When entering a promo code, I get the following : Browser response

AJAX

    function applyPromo(){
        var promo = $("#promo-code-value").val().trim();
        console.log(promo);
        $.ajax({
            url:"get-promo.php",
            type: "POST",
            data:{ getpromo : promo},
            success: function(response){
                console.log("%j", response);
                console.log(response.status);
                if(response.status === "success"){
                    swal("Good job!!", 
                        "Promo code applied Successfully!",
                        "success"
                        ).then(() => {
                            reloadPage();
                        });
                    }else{
                    swal("Humm...", "Promo code no longervalid","error");
                    }   
                }
            })
        }

PHP

if(isset($arr)){
        //Add array to session variable 
        //** Only one code can be apply at the time . will override
        $_SESSION['promo-code'] = $arr; 
        //Success response 
        echo json_encode(array(
            'status' => 'success',
            'message'=> $arr, //For debugging only , will be replaced to success message 
        ));
    }else {
        echo json_encode(array(
            'status' => 'error',
            'message'=> 'error message'
        ));
    }

It's only for a school project. Thanks!!

Ive tried switching "success" to "error" but it is simply not evaluating .I'm new to ajax or php but I believe to have seen others makes it work that way.

Pippo
  • 2,173
  • 2
  • 3
  • 16
IssaMax
  • 11
  • 4
  • The php code that you show is the code that is inside `get-promo.php` ? Because if that's the case, it doesn't make sense to ask for ` $arr ` when what you pass to post is get-promo, you should ask for `isset($_Post['get-promo'])` – Julian Fagadau May 13 '23 at 21:59
  • Hello Julian. That's correct, the php code is inside get-promo.php. Like I commented, it was to get a response on the client side but it will be replaced once I'll be done. – IssaMax May 13 '23 at 22:25

1 Answers1

0

The problem one is that response is in JSON. So you have to convert it into JS object and then you can get status. Here is the updated Code:-

function applyPromo(){
   var promo = $("#promo-code-value").val().trim();
   console.log(promo);
    $.ajax({
      url:"get-promo.php",
      type: "POST",
      data:{ getpromo : promo},
      success: function(response){
         let response = JSON.parse(response);
         console.log("%j", response);
         console.log(response.status);
         if(response.status === "success"){
            swal("Good job!!", 
              "Promo code applied Successfully!",
              "success"
            ).then(() => {
               reloadPage();
            });
         }else{
            swal("Humm...", "Promo code no longervalid","error");
         }   
      }
    })
  }

The second problem is that, you are not using session_start(). Here is the updated code:-

if(isset($arr)){
  //Add array to session variable 
  //** Only one code can be apply at the time . will override
  session_start();
  $_SESSION['promo-code'] = $arr; 
  //Success response 
  echo json_encode(array(
     'status' => 'success',
     'message'=> $arr, //For debugging only , will be replaced to success message 
  ));
}else {
  echo json_encode(array(
    'status' => 'error',
    'message'=> 'error message'
  ));
}
Developer
  • 1,297
  • 1
  • 4
  • 15
  • I tried previously to convert it inside the if statement and it did not work but by putting is value inside a variable and renaming it, it did work, Thanks!! For the session, Session_start is inside an include up in the file .It is present in the whole project and does not cause problem elsewhere – IssaMax May 13 '23 at 22:20
  • @Maxime so now you have only PHP issue?? JavaScript side parsing works. Right?? – Developer May 14 '23 at 02:20
  • For php, I encountered such problem during Ajax. And that issue occur, because request doesn't complete and that's why session doesn't store. But that issue I faced in laravel. And later also on WordPress. But I resolved them. That was due to request was not completing. – Developer May 14 '23 at 02:22
  • @Maxime if you still have the issue in PHP session, then comment me. – Developer May 14 '23 at 02:23
  • 1
    I tried putting it on top of my Session variable like you did and now everything is working fine. Weirdly this is the only way that this is working. I had it previously inside an include() and I even try to put my Session_start on top of the file but with no success..Thanks again!! – IssaMax May 14 '23 at 02:53
  • @Maxime Yes because session is not starting. It happens, the actual reason can be found by looking at the code. But I think its safe, to put session_start again there. If your problem is fixed, marked the answer correct for future visitors. – Developer May 14 '23 at 03:00