tags:

views:

46

answers:

3

I have to place a link to a webpage through an <a> .The webpage link contains some request parameters and I don't want to send it across through the browser directly (meaning I want to use something similar to POST method of FORM).

<a href="www.abc.do?a=0&b=1&c=1>abc</a>

I am using this inside a jsp page and is there a way to post it through javascript or any other way where I don't pass the request parameters through the url?

+4  A: 

You can use links to submit hidden forms, if that's what you're asking.

<a href="#" onclick="submitForm('secretData')">Click Me</a>

<form id="secretData" method="post" action="foo.php" style="display:none">
  <input type="hidden" name="foo" value="bar" />
</form>

<script type="text/javascript">
  function submitForm(formID) {
    document.getElementById(formID).submit();
  }
</script>
Jonathan Sampson
Inline event handlers? Tsk Tsk.. :) +1 Great answer
Doug Neiner
@Doug: Meh, a bit lazy tonight :)
Jonathan Sampson
A: 

You can use AJAX for that. For example, with jQuery, you can use the post function:

<a href="#" id="myMagicLink">clickMe</a>

<script type="text/javascript">
$(function(){
  $("#myMagicLink").click(function(){
    $.post("www.abc.do", {"a":0, "b":1, "c":1});
  });
});
Marius
NO Ajax .Plain javascript only
Harish
Hmm... I wasn't aware AJAX wasn't "plain JavaScript." Unless by plain you mean without a framework. You could make a more re-usable solution using jQuery to pull attributes from an <a> tag and construct the POST request without using hidden forms, while preserving the <a> tag to redirect to an error page for people who don't have JavaScript enabled. That's just my $.02, though.
Zack
A: 

Use the form tag with hidden fields to submit your data:

 <a href="#" onclick="document.frm.submit(); return false">GO !!</a>

 <form name="frm" action="www.abc.do" method="post">
   <input type="hidden" value="your-data" name="whatever">
   <input type="hidden" value="your-data" name="whatever">
   <input type="hidden" value="your-data" name="whatever">
 </form>
Sarfraz