Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

Linking PHP to JSON

Hello

I have a project where I have to use AJAX, PHP and JSON. On the AJAX course, I have learnt how to get data from a JSON file using AJAX.

However, I just started learning PHP and I am not quite sure how PHP is linked to JSON/AJAX. For example, is it possible to edit/generate a JSON file using PHP? Overall, what part does PHP play in the overall process?

I realy would appreciate it if someone could provide some clarity on this or send me a link that explains it.

Thank you.

1 Answer

Miguel Canas
seal-mask
.a{fill-rule:evenodd;}techdegree
Miguel Canas
Python Development Techdegree Student 11,470 Points

Hey Mohammed,

Some basic concepts you may already know:

  • AJAX is used to send a request to a server, and fetch a response without having to refresh your page.
  • JSON is used as a data format that can be understoon by both the client (your browser for example) and a server.
  • PHP is a scripting language used to create dynamic web applications.

In one scenario, you could simply fetch a JSON file from the server using AJAX, but the data you fetch is static. JSON is not a scripting language, it has no logic, it doesn't do anything other than format your data.

Well, what if you wanted to fetch non-static data, like from a database, and receive it in JSON format? You need some other language to handle the process of receiving the request, access the data you asked for from the database, and return it to you in the proper format. That's where a scripting language like PHP comes in handy.

So yep, you're exactly right, PHP can edit/generate the JSON for you, and it can also handle all the routing for your website; handle form requests; AJAX requests; get data in and out of a database; and lot's of other useful things. It's a super handy and fun programming language learn.

Team Treehouse has a nice intro on this very subject: Beginner's Guide to AJAX Development with PHP

Hope that makes sense!

~ Miguel

Mohammed Jammeh
Mohammed Jammeh
37,463 Points

Thank you so much for the reply, Miguel. I was very confused in the beginning but I do have an idea of how the whole thing works now.

So basically the 'index.html' file is connected to to JS file (AJAX). AJAX then loads data/information from PHP which is linked to JSON.

The Intro link was also very helpful. I do appreciate the help :)