Is the only way of passing POST parameters in HTML through a form?

60,647

Solution 1

There are only two ways to POST from a browser - a form, or an Ajax request.

Solution 2

Using HTML only, a form is the only way to generate a POST request. You can use server side scripting / Javascript to generate POST requests in other ways, but no other ways to do with plain HTML only.

Solution 3

As you've already discovered, there are exactly two ways to transmit data over the http protocol: GET or POST. There is also a third type of HTTP message called HEAD, but that's really only used to get the meta data around a resource without downloading it and isn't widely implemented.

Obviously both GET and POST are easily accessible through the use of a <form> tag. The GET is also easily accessible by manually adding query parameters to the URL in the form of name-value pairs (foo.html?a=1&b=2).

The beauty and complexity of the POST, however, is that the name-value pairs are communicated from the browser to the web server enclosed within the HTTP request header which is not as easily accessible. The only way to accomplish the POST without using a <form> tag is to manually alter the HTTP request header and add the name-value pairs in yourself.

Also keep in mind that an HTTP server doesn't intrinsically know whether a request (GET or POST) came from a main browser window or an AJAX call. Regardless, the web server will read the request, will decipher if it's a GET or POST request, look for name-value pairs as appropriate, and generate a response.

If you'd like additional detail on how to properly format a POST request you can go to jmarshall.com/easy/http/ or perhaps tcpipguide.com/free/t_HTTPRequestMessageFormat.htm. The definitive resource is always the W3C, but sometimes the RFCs can be terribly confusing for us mere mortals to read.

Solution 4

In HTML only it's with a form.

But you can do it if you play with your server side. Here is a good article that show you how to manipulate the Get to Change it to Post via PHP. This will require you to play with fsockopen... This way to do it will use your parameter ?id=1&param=2 ... and will create a POST request on the server side. You can make it generic, once it setups it will works, but it's a little work to setup everything first.

Share:
60,647
Philip Morton
Author by

Philip Morton

Updated on July 15, 2022

Comments

  • Philip Morton
    Philip Morton almost 2 years

    In HTML, you can send data from one page to another using a GET request in a couple of ways:

    http://www.example.com/somepage.php?data=1
    

    ...or...

    <form action="somepage.php" method="get">
      <input type="hidden" name="data" value="1" />
      <input type="submit" value="Submit">
    </form>
    

    With a POST request though, I've only seen data being sent through form elements like this:

    <form action="somepage.php" method="post">
      <input type="hidden" name="data" value="1" />
      <input type="submit" value="Submit">
    </form>
    

    If I only have one parameter I want to send to another page using POST, is there an easier way than wrapping it in a form?