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 }); } }); } 
3
  • What is happening in 'php/validation.php'? I would guess that function is always returning 1. That's the first place to look Commented Apr 12, 2018 at 20:14
  • `"... There are no errors ... "*. You are contradicting yourself. Commented Apr 12, 2018 at 20:14
  • 2
    make sure to use if(data == 0) instead of if(data = 0). One = assigns the value to the variable while two compares them. Commented Apr 12, 2018 at 20:15

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

see the paste bin i added to the answer above. I am getting the error invalid user
I think that you have empty $_SESSION['userId']
0

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

2 Comments

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.) 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
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.