{"id":1949,"date":"2019-09-19T12:00:00","date_gmt":"2019-09-19T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/get-request-to-php-api-using-jquery-part-2\/"},"modified":"2026-07-05T03:24:33","modified_gmt":"2026-07-05T01:24:33","slug":"get-request-to-php-api-using-jquery-part-2","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/get-request-to-php-api-using-jquery-part-2\/","title":{"rendered":"GET Request to PHP API Using JQuery \u2013 Part 2"},"content":{"rendered":"<p>This tutorial follows from <a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-to-write-a-rest-api-in-php-and-mysql-step-by-step\/\" target=\"_blank\" rel=\"noopener noreferrer\">Part 1.<\/a><\/p>\n<p>In this tutorial, you would learn how to call the PHP API from a html page. We would use JQuery to make the REST API call. Then the data returned will be displayed on\u00a0 html page.<\/p>\n<p><strong>Note:<\/strong>\u00a0Later I would show you an easier way to fectch MySQL data. That is using PDO. But for now, try learn how it works!<\/p>\n<p>The three steps are:<\/p>\n<ol>\n<li><a href=\"#t1\">Create the HTML Page<\/a><\/li>\n<li><a href=\"#t2\">Write the JQuery Script<\/a><\/li>\n<li><a href=\"#t3\">Test the API<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong id=\"t1\">1. Create the HTML Page<\/strong><\/p>\n<p>Inside the same folder as the services.php, create html file. Name it index.html<\/p>\n<p>The file should have the following structure:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\n<span style=\"color: #007700;\">&lt;head&gt;<\/span>\n<span style=\"color: #007700;\">&lt;title&gt;<\/span>Consume PHP Web Service<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\n\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"https:\/\/code.jquery.com\/jquery-3.4.1.min.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\n<span style=\"color: #007700;\">&lt;script&gt;<\/span> \n\t<span style=\"color: #888888;\">\/\/ JQuery coded goes here<\/span>\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\n\n<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\n\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>\n\n<span style=\"color: #007700;\">&lt;div&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;h2&gt;<\/span> List of Students <span style=\"color: #007700;\">&lt;\/h2&gt;<\/span>\t\n\t<span style=\"color: #007700;\">&lt;ol<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"students\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\n\t<span style=\"color: #888888;\">&lt;!-- list goes here --&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;\/ol&gt;<\/span>\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\n\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Write the JQuery Script<\/strong><\/p>\n<p>Now, inside the second &lt;Script&gt;&lt;\/Script&gt; tag, write the code to make a rest call to the services.php. The code fetches the data and encodes it as a json. Finally it appends it as &lt;li&gt;&lt;\/li&gt; items to the students elements.<\/p>\n<p>The full code is given below. You need to put this code in the &lt;script&gt;&lt;\/script&gt; tag.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ JQuery coded goes here<\/span>\n$.get(<span style=\"background-color: #fff0f0;\">\"http:\/\/localhost\/demoweb\/services.php\"<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>(data, status){\n<span style=\"color: #888888;\">        \/\/ Parse the data and save it in studentList variable<\/span>\t\n<span style=\"color: #008800; font-weight: bold;\">        var<\/span> studentList <span style=\"color: #333333;\">=<\/span> JSON.parse(data);\n\t<span style=\"color: #008800; font-weight: bold;\">for<\/span>(<span style=\"color: #008800; font-weight: bold;\">var<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>; i <span style=\"color: #333333;\">&lt;<\/span> studentList.length; i<span style=\"color: #333333;\">++<\/span>) {\n\t\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> student <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"id: \"<\/span> <span style=\"color: #333333;\">+<\/span> studentList[i].id <span style=\"color: #333333;\">+<\/span>\n\t\t\t\t\t  <span style=\"background-color: #fff0f0;\">\" firstname: \"<\/span> <span style=\"color: #333333;\">+<\/span> studentList[i].firstname <span style=\"color: #333333;\">+<\/span>\n\t\t\t\t\t  <span style=\"background-color: #fff0f0;\">\" lastname: \"<\/span> <span style=\"color: #333333;\">+<\/span> studentList[i].lastname <span style=\"color: #333333;\">+<\/span>\n\t\t\t\t\t  <span style=\"background-color: #fff0f0;\">\" location: \"<\/span> <span style=\"color: #333333;\">+<\/span> studentList[i].location;\n\t\t<span style=\"color: #888888;\">\/\/add the item to page as list items &lt;li&gt;<\/span>\n\t\tstudent <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;li&gt;\"<\/span> <span style=\"color: #333333;\">+<\/span> student <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;\/li&gt;\"<\/span>;\n\t\t$(<span style=\"background-color: #fff0f0;\">\"#students\"<\/span>).append(student);\n\t}\n});\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">3. Test the API<\/strong><\/p>\n<p>At this point, you can copy over the files to the server folder. We created this folder in <a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-to-write-a-rest-api-in-php-and-mysql-step-by-step\/\" target=\"_blank\" rel=\"noopener noreferrer\">Part 1<\/a>.<\/p>\n<p>Open the browser and visit, http:\/\/localhost\/demoweb\/index.html<\/p>\n<p>You will see the list of items fetched from the database as shown below<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/List-of-Students.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1242 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/List-of-Students.jpg\" alt=\"\" width=\"828\" height=\"530\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Next Steps<\/strong><\/p>\n<p>Next, we would write a PHP method to make a POST request. This would insert an student record into the MySQL database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial follows from Part 1. In this tutorial, you would learn how to call the PHP API from a html page. We would use &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1949","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1949"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1949\/revisions"}],"predecessor-version":[{"id":2117,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1949\/revisions\/2117"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}