{"id":1950,"date":"2019-09-19T12:00:00","date_gmt":"2019-09-19T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/post-request-to-php-rest-api-with-jquery-part-3\/"},"modified":"2026-07-05T03:24:35","modified_gmt":"2026-07-05T01:24:35","slug":"post-request-to-php-rest-api-with-jquery-part-3","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/post-request-to-php-rest-api-with-jquery-part-3\/","title":{"rendered":"POST Request to PHP REST API With JQuery \u2013 Part 3"},"content":{"rendered":"<p>This tutorial follows from Part 1 and Part 2<\/p>\n<p><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 &#8211; Create a REST API in PHP<\/a><\/p>\n<p><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/get-request-to-php-api-using-jquery-part-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">Part 2 &#8211; Make a GET API Request Using JQuery<\/a><\/p>\n<p>You will learn how to write a method that makes a POST Request to the PHP REST API we created.<\/p>\n<p>So we&#8217;ll cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Create the HTML form<\/a><\/li>\n<li><a href=\"#t2\">Write the JQuery method<\/a><\/li>\n<li><a href=\"#t3\">Modify the Services.php file<\/a><\/li>\n<li><a href=\"#t4\">Test the API<\/a><\/li>\n<li><a href=\"#t5\">Next Steps<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong id=\"t1\">1. Create the HTML Form<\/strong><\/p>\n<p>Open the index.html for we created in Part 2<\/p>\n<p>Create the html for with 3 inputs textboxes and on Submit button. I have put this codes inside a &lt;div&gt;&lt;\/div&gt; tag,<\/p>\n<p>The complete code for the form is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"insertDiv\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;h2&gt;<\/span>Insert New Student<span style=\"color: #007700;\">&lt;\/h2&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;label&gt;<\/span>Firstname: <span style=\"color: #007700;\">&lt;\/label&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"txtFirstname\"<\/span> <span style=\"color: #007700;\">\/&gt;&lt;br&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;label&gt;<\/span>Lastname: <span style=\"color: #007700;\">&lt;\/label&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"txtLastname\"<\/span> <span style=\"color: #007700;\">\/&gt;&lt;br&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;label&gt;<\/span>Location: <span style=\"color: #007700;\">&lt;\/label&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"txtLocation\"<\/span> <span style=\"color: #007700;\">\/&gt;&lt;br&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;br&gt;<\/span>\n\t<span style=\"color: #007700;\">&lt;input<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"button\"<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"saveStudent\"<\/span> <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">\"Save Student\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Write the JQuery Method<\/strong><\/p>\n<p>At this point, we would now write a JQuery method the performs the POST request. This method runs on button-click for the &#8220;saveStudent&#8221; button.<\/p>\n<p>Remember that to select the button, you use the # selector. So it would be #saveButton.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$(<span style=\"background-color: #fff0f0;\">\"#saveStudent\"<\/span>).click(<span style=\"color: #008800; font-weight: bold;\">function<\/span>(){\n\tconsole.log(<span style=\"background-color: #fff0f0;\">\"Inserting data...\"<\/span>)\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> fname <span style=\"color: #333333;\">=<\/span> $(<span style=\"background-color: #fff0f0;\">\"#txtFirstname\"<\/span>).val();\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> lname <span style=\"color: #333333;\">=<\/span> $(<span style=\"background-color: #fff0f0;\">\"#txtLastname\"<\/span>).val();\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> lctn <span style=\"color: #333333;\">=<\/span> $(<span style=\"background-color: #fff0f0;\">\"#txtLocation\"<\/span>).val();\n\n\t<span style=\"color: #888888;\">\/\/Create a new Student object using the values from the textfields<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">var<\/span> student <span style=\"color: #333333;\">=<\/span> {\n\t\tfirstname <span style=\"color: #333333;\">:<\/span> fname,\n\t\tlastname <span style=\"color: #333333;\">:<\/span> lname,\n\t\tlocation <span style=\"color: #333333;\">:<\/span> lctn\n\t};\n\t\n\t<span style=\"color: #888888;\">\/\/Make the POST request using $.post() to the same url<\/span>\n\t$.post(<span style=\"background-color: #fff0f0;\">\"http:\/\/localhost\/demoweb\/services.php\"<\/span>, student, <span style=\"color: #008800; font-weight: bold;\">function<\/span>(data){\n\t\tconsole.log(data);\n\t});\n});\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code above would be inside the document.ready() function. But after the code for the GET request.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. Modify the services.php File<\/strong><\/p>\n<p>Now, we need to modify the services.php file to also accept a post request. So we would use an if statement to check the request method. If the method is GET, then we execute existing code, but if it it POST, we execute the code for handling POST.<\/p>\n<p>The entire content of the services.php file is given below. I have used comments to separate different sections<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\n   <span style=\"color: #996633;\">$url<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"localhost:3301\"<\/span>;\n   <span style=\"color: #996633;\">$database<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"demodb\"<\/span>;\n   <span style=\"color: #996633;\">$username<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"root\"<\/span>;\n   <span style=\"color: #996633;\">$password<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"root\"<\/span>;\t\n\n   <span style=\"color: #996633;\">$conn<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">mysqli_connect<\/span>(<span style=\"color: #996633;\">$url<\/span>, <span style=\"color: #996633;\">$username<\/span>, <span style=\"color: #996633;\">$password<\/span>, <span style=\"color: #996633;\">$database<\/span>);\n\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span><span style=\"color: #996633;\">$conn<\/span>) {\n\t<span style=\"color: #008800; font-weight: bold;\">die<\/span>(<span style=\"background-color: #fff0f0;\">\"Unable to connect: \"<\/span> <span style=\"color: #333333;\">.<\/span> <span style=\"color: #996633;\">$conn<\/span><span style=\"color: #333333;\">-&gt;<\/span><span style=\"color: #0000cc;\">connect_error<\/span>);\n   }\n   \n   <span style=\"color: #996633;\">$method<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #996633;\">$_SERVER<\/span>[<span style=\"background-color: #fff0f0;\">'REQUEST_METHOD'<\/span>];\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #996633;\">$method<\/span> <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">\"GET\"<\/span>) {\n\n   \t   <span style=\"color: #888888;\">\/\/************** START OF GET REQUEST ************<\/span>\n\t   <span style=\"color: #996633;\">$sql<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"SELECT * FROM demodb.tblusers\"<\/span>;\n\t   <span style=\"color: #996633;\">$results<\/span> <span style=\"color: #333333;\">=<\/span> mysqli_query(<span style=\"color: #996633;\">$conn<\/span>, <span style=\"color: #996633;\">$sql<\/span>);   \n\n\t   <span style=\"color: #996633;\">$rows<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">array<\/span>();\n\n\t   <span style=\"color: #008800; font-weight: bold;\">if<\/span>(mysqli_num_rows(<span style=\"color: #996633;\">$results<\/span>) <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>){\n\t\t<span style=\"color: #008800; font-weight: bold;\">while<\/span>(<span style=\"color: #996633;\">$record<\/span> <span style=\"color: #333333;\">=<\/span> mysqli_fetch_assoc(<span style=\"color: #996633;\">$results<\/span>)) {\n\t\t\t<span style=\"color: #007020;\">array_push<\/span>(<span style=\"color: #996633;\">$rows<\/span>, <span style=\"color: #996633;\">$record<\/span>);\n\t\t}\t\t\n\t\t<span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"color: #007020;\">json_encode<\/span>(<span style=\"color: #996633;\">$rows<\/span>);\n\t   }\n\t   <span style=\"color: #008800; font-weight: bold;\">else<\/span> {\n\t\t  <span style=\"color: #008800; font-weight: bold;\">echo<\/span>(<span style=\"background-color: #fff0f0;\">\"No records found\"<\/span>);\n\t   }\n   }\n   <span style=\"color: #888888;\">\/\/*********** END OF GET REQUEST *************<\/span>\n\n  <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #996633;\">$method<\/span> <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">\"POST\"<\/span>)\n  {\n\t  <span style=\"color: #996633;\">$firstname<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #996633;\">$_POST<\/span>[<span style=\"background-color: #fff0f0;\">\"firstname\"<\/span>];\n  \t  <span style=\"color: #996633;\">$lastname<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #996633;\">$_POST<\/span>[<span style=\"background-color: #fff0f0;\">\"lastname\"<\/span>];\n\t  <span style=\"color: #996633;\">$location<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #996633;\">$_POST<\/span>[<span style=\"background-color: #fff0f0;\">\"location\"<\/span>];\n\t  \n\t  <span style=\"color: #996633;\">$sql_insert<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"INSERT INTO demodb.tblusers(id, firstname, lastname, location) <\/span>\n<span style=\"background-color: #fff0f0;\">\t  VALUES (0, '<\/span><span style=\"background-color: #eeeeee;\">$firstname<\/span><span style=\"background-color: #fff0f0;\">', '<\/span><span style=\"background-color: #eeeeee;\">$lastname<\/span><span style=\"background-color: #fff0f0;\">', '<\/span><span style=\"background-color: #eeeeee;\">$location<\/span><span style=\"background-color: #fff0f0;\">')\"<\/span>;\n\t  \n\t  <span style=\"color: #008800; font-weight: bold;\">if<\/span>(mysqli_query(<span style=\"color: #996633;\">$conn<\/span>, <span style=\"color: #996633;\">$sql_insert<\/span>)) {\n\t\t  <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"Student data was succesfully inserted into the database!\"<\/span>;\n\t  }\n\t  <span style=\"color: #008800; font-weight: bold;\">else<\/span> {\n\t\t  <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"Error occured. Data was not insered : \"<\/span><span style=\"color: #333333;\">.<\/span> mysqli_error(<span style=\"color: #996633;\">$conn<\/span>);\n\t  }\t\t  \t\t  \n  }\n\t \n  mysqli_close();\n<span style=\"color: #557799;\">?&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Test the API<\/strong><\/p>\n<p>You can now upload all the files to the server.<\/p>\n<p>Then visit http:\/\/localhost\/demoweb\/index.html<\/p>\n<p>You will see the form as shown below.<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Post-Request-Page.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1247 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Post-Request-Page.jpg\" alt=\"\" width=\"638\" height=\"458\" \/><\/a><\/p>\n<p>Fill up the form and submit. Then refresh the page to see the new data submitted. If it works, thumbs up to you!<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t5\">5. Next Steps<\/strong><\/p>\n<p>If you completed this part, then congrats!<\/p>\n<p>Next, we would learn how to make a PUT request and a DELETE request.<\/p>\n<p>Finally, we would now go on to see how to do all this an easy way!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial follows from Part 1 and Part 2 Part 1 &#8211; Create a REST API in PHP Part 2 &#8211; Make a GET API &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":[289],"tags":[],"class_list":["post-1950","post","type-post","status-publish","format-standard","hentry","category-rest-web-services"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1950","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=1950"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1950\/revisions"}],"predecessor-version":[{"id":2118,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1950\/revisions\/2118"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}