0

I know that I cannot make a POST directly through < a> tag, but can I wrap it in a form, like:

<form action="theUrl" method="POST">

    <input type="hidden" name="param1" value="key1" />
    <a href="/url1" onclick="submit()">key1</a>

    <input type="hidden" name="param2" value="key2" />
    <a href="/url2" onclick="submit()">key2</a>

</form>

In this way, when I click on the url1, how can the form make a POST with the value='key1'? Thank you very much!

Robert
  • 1,789
  • 5
  • 29
  • 37
  • This isn't a question... it's a post. An appropriate format would be to pose a question and then answer it yourself... or to find a question for which this is an answer and add it as an answer. – Dancrumb Aug 06 '13 at 16:44
  • Possibly related [question](http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) –  Aug 06 '13 at 16:44
  • use 2 separate forms. – Daniel A. White Aug 06 '13 at 16:45

2 Answers2

2

Clicking either of the <a>s will POST both keys. The simplest solution is to have two <form>s. Here:

<form action="theUrl" method="POST">

    <input type="hidden" name="param1" value="key1" />
    <a href="/url1" onclick="this.parentNode.submit()">key1</a>

</form>

<form action="theUrl" method="POST">

    <input type="hidden" name="param2" value="key2" />
    <a href="/url2" onclick="this.parentNode.submit()">key2</a>

</form>

Alternatively, you can do the following to submit the forms:

<script type="text/javascript">
 function s(x)
 {
   x.parentNode.submit();
 }
</script>

<form action="theUrl" method="POST">

    <input type="hidden" name="param1" value="key1" />
    <a href="/url1" onclick="s(this)">key1</a>

</form>

<form action="theUrl" method="POST">

    <input type="hidden" name="param2" value="key2" />
    <a href="/url2" onclick="s(this)">key2</a>

</form>
Nerius
  • 790
  • 4
  • 16
0

Call js function in onclick of a tag. And then submit the form inside the function.

    <a href="javascript:void(0);" onclick="submitform()">

    <script type="text/javascript">  
       function submitform()  {
          document.formName.submit();
       }
    </script>
shakthydoss
  • 2,341
  • 3
  • 24
  • 34