Javascript fetch data from php
I am trying to fetch values from php file Show api.php
my javascript page has code like this.(this code is not on page api.php)
can you please guide me how to fetch value of variable from php file by using fetch. asked Sep 9, 2016 at 14:33
Manpreet OberoiManpreet Oberoi 3473 gold badges4 silver badges11 bronze badges 3 You are not echoing the value, so it won't be send.
answered Sep 9, 2016 at 14:36
7 Your php needs to output the value you want. A simple way to do this is to use
answered Sep 9, 2016 at 14:40
RickCoxDevRickCoxDev 1651 silver badge9 bronze badges 1 You should use echo to display it in php then it will be available in you JavaScript reponse. answered Aug 26, 2019 at 1:35
TL;DR How can I use fetch() to return data from a PHP file? Hi all. I am having difficulty using
How can I now use I've tried the JS below:
But this fails on the In the
above code JS code, if I replace the file (
then the data from the JSON file is indeed written to the console as expected. I realize there are other ways of using fetch(), and I will indeed learn those ways. But right now I am hoping to learn this method, since these use the tools I have readily available to me. Once I get this method working, I will continue to learn the basics of async/await with fetch. After that, I can branch out to other ways of using fetch() (aside from with a PHP file). Thanks in advance. Edit: Issue resolved. /u/cawcvs pointed out that my PHP file needs to echo the JSON array. So I added the line In my career, I have used both AJAX and Fetch API (more recently). If you have ever had to get remote data from an API or internal PHP URL, most likely you have used one or the other. In this tutorial, I show you how to do both methods just in case you are only familiar with one or the other. We will be using PHP to create a JSON object that grabs a list of all countries (if I missed one, I am sorry, I copied a list). AJAX has been tried and tested for years within jQuery and at one point was the go-to method. With Vanilla JS making a considerable comeback, new options like the built-it Fetch API are used more often. This is mostly due to its ease of use and since it is built into JavaScript, you no longer need a framework or library to use it. View This On YouTubeFile Structure/index.html For simplicity, I will be pulling from a local PHP file that returns a JSON object that can then be consumed using AJAX or Fetch. You can use this PHP file for both methods. In the init.js file, you would add your AJAX or Fetch code listed below. HTML Setup
Enter fullscreen mode Exit fullscreen mode If you are using the AJAX method, you can add a reference to jQuery directly above the
Enter fullscreen mode Exit fullscreen mode Setup Our PHP FileIn your First, we need to add an array of countries. You can find such arrays on GitHub but mine should be pretty
extensive. My entire PHP file is below so you can see where it goes.
Enter fullscreen mode Exit fullscreen mode The PHP code should now be set for a response using AJAX or Fetch. It is simple code and can be replaced with a direct call to an API if you want, but to make this tutorial more simple, we are just calling a PHP file. AJAX MethodIn your NOTE: Make sure you have the jQuery tag added to your
Enter fullscreen mode Exit fullscreen mode Using the code above, we initiate an AJAX call on the The on-click event for the Fetch API MethodNOTE: This method uses built-in functions on Vanilla JS so you don't need to add any references to jQuery.
Enter fullscreen mode Exit fullscreen mode So, if you are comparing AJAX to Fetch API, they are very similar. The code between the two almost mirror each other but one uses jQuery functions and the other uses JS functions. Using the code above, we initiate a Fetch API call on the The
on-click event for the ConclusionPersonally, I like Fetch API. I used jQuery for so long, that I forgot how to use Vanilla JS, but once I was able to really get back into it I felt that it is better since it is faster and has a much smaller footprint on my servers. You can use either depending on your needs. Fetch is riding the comeback of Vanilla JS and gives you a lot more flexibility if you plan on using other frameworks outside of jQuery (React, Vue, etc). |