I'm not sure if this is even possible. But I was wondering if anyone knows how to make a hyperlink pass some variables and use POST (like a form) as opposed to GET.
You create a form with hidden inputs that hold the values to be posted, set the action of the form to the destination url, and the form method to post. Then, when your link is clicked, trigger a JS function that submits the form.
See here, for an example. This example uses pure JavaScript, with no jQuery — you could choose this if you don't want to install anything more than you already have.
<form name="myform" action="handle-data.php" method="post">
<label for="query">Search:</label>
<input type="text" name="query" id="query"/>
<button>Search</button>
</form>
<script>
var button = document.querySelector('form[name="myform"] > button');
button.addEventListener(function() {
document.querySelector("form[name="myform"]").submit();
});
</script>
Why is this the accepted answer? The question specifically asks for use POST (like a form) as opposed to GET, yet this answer says "set action to the destination url and method to get".
I get that. What I don't understand is this: we go into the trouble of creating a hidden form so that we can POST, but then, after the form is created, why you propose to set the method to
GET
? We hadget
without the form, didn't we?You are right. That was an error, but somehow the OP still got it right (probably because of the included link which is much clearer). Thanks for pointing out. I rephrased that entirely.
I updated your answer to use more modern approach. If you disagree, feel free to rollback this edit.
There is no reason to involve Javascript in this process. Why is this the accepted answer?