javascripthtmlhttp

Make a link use POST instead of GET


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.


Solution

  • 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: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml (this example uses pure js, 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">
    Search: <input type='text' name='query' />
    <a href="javascript: submitform()">Search</a>
    </form>
    <script type="text/javascript">
    function submitform()
    {
      document.myform.submit();
    }
    </script>