From the course: Ajax with PHP: Add Dynamic Content to Websites
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Mark favorites on page load
From the course: Ajax with PHP: Add Dynamic Content to Websites
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.