Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass POST parameters in a URL?

Basically, I think that I can't, but I would be very happy to be proven wrong.

I am generating an HTML menu dynamically in PHP, adding one item for each current user, so that I get something like <a href="process_user.php?user=<user>>, but I have a preference for POST over GET.

Is there a way to pass the information as a POST parameter, rather than GET from a clickable HREF link?

I am not allowed to use JavaScript.


It looks like Rob is on to something with "You could use a button instead of an anchor and just style the button to look like a link. That way you could have your values in hidden fields inside the same form to be sent via POST"

like image 719
Mawg says reinstate Monica Avatar asked Jun 02 '11 05:06

Mawg says reinstate Monica


People also ask

Can we pass URL parameters in POST request?

If you know the URL parameters for your form post when the HTML page is sent to the client, you can tack those URL parameters on to the form's action attribute, otherwise JavaScript can set the URL parameters when the form is submitted.

How do I pass parameters to URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

Can we pass parameters in POST method?

In a POST request, the parameters are sent as a body of the request, after the headers. To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.

How do I send a POST request URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.


2 Answers

You could use a form styled as a link. No JavaScript is required:

<form action="/do/stuff.php" method="post">     <input type="hidden" name="user_id" value="123" />     <button>Go to user 123</button> </form> 

CSS:

button {     border: 0;     padding: 0;     display: inline;     background: none;     text-decoration: underline;     color: blue; } button:hover {     cursor: pointer; } 

See: http://jsfiddle.net/SkQRN/

like image 102
Petah Avatar answered Sep 28 '22 07:09

Petah


Parameters in the URL are GET parameters, a request body, if present, is POST data. So your basic premise is by definition not achievable.

You should choose whether to use POST or GET based on the action. Any destructive action, i.e. something that permanently changes the state of the server (deleting, adding, editing) should always be invoked by POST requests. Any pure "information retrieval" should be accessible via an unchanging URL (i.e. GET requests).

To make a POST request, you need to create a <form>. You could use Javascript to create a POST request instead, but I wouldn't recommend using Javascript for something so basic. If you want your submit button to look like a link, I'd suggest you create a normal form with a normal submit button, then use CSS to restyle the button and/or use Javascript to replace the button with a link that submits the form using Javascript (depending on what reproduces the desired behavior better). That'd be a good example of progressive enhancement.

like image 38
deceze Avatar answered Sep 28 '22 06:09

deceze