-1

I am bit confused on a task which involves backend web developement: I have to make simple contact us form backend solution 1st task involved form-data post method, but i am not really sure what to do on the 2nd task

Sending JSON data as body element using HTTP POST method

  • You need to make an API service. You need to send the data via json, which you then you can decode in PHP, and you need to also have a html form to submit data. Basically you need to be able to post data via API and form on the webpage. – Arthur May 30 '17 at 19:17
  • 1
    I'm voting to close this question as off-topic because it is a homework task list! – AbraCadaver May 30 '17 at 19:23
  • Why close it? I'm asking if i understood the task right, not for a complete solution. TLDR : guidance – mulvikciks May 30 '17 at 19:27
  • @Arthur I should use cURL library in order to send data as json via http post method? https://lornajane.net/posts/2011/posting-json-data-with-php-curl. Also what is the difference between posting data through api service and form? – mulvikciks May 30 '17 at 19:30
  • @mulvikciks I usually use cURL so you can do that. Also for testing you can use postman. You can set the url to post data to, what to post and it also has the option to export to code, if you want to be lazy. Also, don't forget to parse and verify all the data the user sends and respond with errors if something is wrong, if everything is good, return a success message. – Arthur May 30 '17 at 19:32
  • @Arthur Thanks alot, last question from my side. Also what is the difference between posting data through api service and form? i'm quite new to web developement, so i'm bit confused overall – mulvikciks May 30 '17 at 19:36
  • @mulvikciks If you do it through a form the user needs to be on the site. If you have an api anybody can send data without being on your web page. So anybody could send data to you from their code. – Arthur May 30 '17 at 19:41

1 Answers1

-2

For your first question:

1. form-data POST method using simple HTML form:

HTML :

<form action="/action_page.php" method="post">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

other option is:

JS :

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

or using $.post

check out jquery ajax / post: jquery post

And for the 2nd question:

2. Sending JSON data as body element using HTTP POST method

For backend soultion you will need to create an api on your server and call to his methods from your client side. The api will get the json data in post, parse it and will run the logics that include db on the server-side after that the api will return the answer to the client. (In this way the client can use function that include db without "knowing" anything about the db, it just need the right url).

Creating a REST API using PHP or if you preferre c#: web-api

itay
  • 357
  • 4
  • 16