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.

Respond with JSON data

Respond with JSON data

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

Start my 1-month free trial

Respond with JSON data

- [Instructor] In the previous movie we used PHP to be able to return text or HTML. In this movie, let's learn how we can use PHP to send back JSON data as the response. The reason that we might want to use JSON data, is because it allows us to return structured and labeled data, and that can be really useful. We can then pass that data back to JavaScript, and it's ready for use in JavaScript. JavaScript can iterate through it, it can pick out the pieces that it needs, and then it can use that information, that data that we've passed back in order to make changes to our page. If we're only passing back a single item, like a bit of text or a bit of HTML, then it makes sense to do it that way, but if we want to pass it back multiple pieces of data, then JSON is probably the way we want to go. The way that we would write JSON data in our PHP page is the same as we did for HTML. You write JSON in order to return JSON. Whatever you put in that page, is what's going to get returned. Now…

Contents