tags:

views:

2484

answers:

5

Hi,

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?

Thanks in advance.

+7  A: 

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

Greg
+3  A: 

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.

Eran Galperin
+1  A: 

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.

Daok
I think his intention was to create a POST request to avoid sending the parameters over the address line.. which your method defeats.
Eran Galperin
When I read his question I understand that he wants to send stuff within the url and not a form and proceed all via POST... but I might be wrong... question is unclear than... anyways what I suggest works if the OP want to do what I suggest.
Daok
+1  A: 

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.

Ed Lucas
A: 

You can of course always do a GET to a page which contains server-side (or AJAX) logic which will create a POST request (e.g. GET /pageWhichCreatesAPost.py). Very messy of course, but there can be cases where such a work-around could maybe be useful.

This is exactly what I wrote 1 hour ago :P
Daok