How can I pass POST parameters in a URL?
Solution 1
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/
Solution 2
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.
Solution 3
You can make a link perform an Ajax post request when it's clicked.
In jQuery:
$('a').click(function(e) {
var $this = $(this);
e.preventDefault();
$.post('url', {'user': 'something', 'foo': 'bar'}, function() {
window.location = $this.attr('href');
});
});
You could also make the link submit a POST form with JavaScript:
<form action="url" method="post">
<input type="hidden" name="user" value="something" />
<a href="#">CLick</a>
</form>
<script>
$('a').click(function(e) {
e.preventDefault();
$(this).parents('form').submit();
});
</script>
Solution 4
I would like to share my implementation as well. It does require some JavaScript code though.
<form action="./index.php" id="homePage" method="post" style="display: none;">
<input type="hidden" name="action" value="homePage" />
</form>
<a href="javascript:;" onclick="javascript:
document.getElementById('homePage').submit()">Home</a>
The nice thing about this is that, contrary to GET requests, it doesn't show the parameters in the URL, which is safer.
Solution 5
No, you cannot do that. I invite you to read a POST definition.
Or this page: HTTP, request methods
Mawg says reinstate Monica
Donate a cup of food for free: Click to Give @ The Hunger Site SOreadytohelp
Updated on July 09, 2022Comments
-
Mawg says reinstate Monica almost 2 years
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"
-
Rob almost 13 yearsThis works. Just to add if he wanted the link to be consistent with his other links, he might add it in his stylesheet as
a:link, button.class {}
, anda:hover, button.class:hover {}
, etc. -
Rob almost 13 yearsThough you link to some informative articles, it actually is possible with some JavaScript or CSS as shown in other answers.
-
zzarbi almost 13 yearsYou question is "How to pass POST parameters in a URL ?" the answer is your CANNOT. The previous code is to send POST variables through a form via a javascript event...
-
Rob almost 13 yearsThat was the title of the question. If you read the actual question, he was looking for something different.
-
zzarbi almost 13 years"Is there any way to pass the info as a POST parameter, rather than GET from a clickable HREF link?" as a clickable LINK... we can play on word I agree that you can hack around it but it's not a link anymore, it's an action.
-
Mawg says reinstate Monica almost 13 yearsok, alright, already ... a clickable anything (which the user might think is a link, would be a bonus) It looks like @rob has an idea with submit buttons
-
Code Maverick about 9 yearsperfect thanks!!!!!! +1 works like a charm. adding
target="_blank"
is even better. -
Alphaaa about 9 years...and you can still set
method='post'
to the form, if you like. Great! -
Peter Mortensen over 3 yearsThis is not much more than RTFM. Can you provide a summary and/or an actual argument for why not in the answer? (Without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)