0

I am trying to give feedback to a user. If they have added something already they will get a message saying that This attraction has already been added to your itinerar. If a user doesnt have the attraction in their itinerary they will get a success message. Although it always says that the attraction has already been added, even if it hasn't. There are no errors, does anyone know why this keeps happening. Below is the code for the function.

function addAttractionItinerary(ev) {
 var id=$(ev.target).parents('div.Attraction')[0].$data.attraction_id;
$.post('php/validation.php', {"CHECKRECORD" :1 ,"id" : id}, function(data){
if (data = 1) {
alert("This attraction has already been added to your itinerary")
handleModalClose();
} else if  (data = 0){ 
         $.ajax({
    url: 'php/itinerary.php',
    type: 'POST', //will send a POST request to the PHP back-end with the "attractionId" the user has clicked on
    dataType: 'json',
    data: {
        action: 'CREATE',
        attractionId: $(ev.target).parents('div.Attraction')[0].$data.attraction_id //The $(ev.target).parents('div.attraction')[0].$data.attraction_id expression get's the "attractionId" of the clicked element
    },
    //It first get's the parent div with the class "attraction" reads the first one in the list [0] and then reads the $data property of this element
    success: function(data) {
 alert("The attraction is successfully added");
},
    error: console.log
}); 
    }
});
}
df123
  • 3
  • 1
  • 6

2 Answers2

0

You should use if (data == 1) {} and if (data == 0) {} instead of if (data = 1) {}, because that sets the data variable to 1.

http://php.net/manual/en/control-structures.if.php http://php.net/manual/en/language.operators.comparison.php

Can I define a variable in a PHP if condition?

Rockey
  • 393
  • 4
  • 18
0

Make sure to use if(data == 0) instead of if(data = 0). One = assigns the value to the variable while two compares them

  • Ok thank you, but when I add this, it comes back with an error saying invalid user. I have added my php files in this paste bin. (they are too large to add to the question.) https://pastebin.com/GHNJVJAX On the itinerary.php it has the error that is coming up, but not sure what is wrong with it. i am logged in by the way – df123 Apr 12 '18 at 20:33
  • That's unrelated to the previous problem. It's saying invalid user because there is no userId variable set in the session. So you need to figure out why that isn't being set – Charlie Heflin Apr 13 '18 at 15:05