From the course: Ajax with PHP: Add Dynamic Content to Websites

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Mark favorites on page load

Mark favorites on page load

From the course: Ajax with PHP: Add Dynamic Content to Websites

Start my 1-month free trial

Mark favorites on page load

- [Instructor] In the previous movie, we learned how to handle the AJAX response and to provide a visual indicator in our blog post to let us know when it had been favorited. However, we also realized there was a problem. And that is that, even though I can click the favorite button and it'll put the red heart there for me. If I reload the page, that red heart is lost. And the reason why, is because the AJAX response is putting favorite in the class for the blog post, but when the page first loads there's no code checking to see if something has been favorited when it renders it for the first time. So the AJAX has gotten slightly more advanced than the HTML code. Now, let's add some code so it will always take into account whether or not something has been favorited in the past. Up here at the top of our index.php page, right after we initialize the session, I'm going to write a new function, "is_favorite." I'm going to pass in an id and it's just going to return true or false…

Contents