Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the POST Method with HTML Anchor Tags

I am certain the answer will be 'NO', but I wanted to ask anyway just incase I have missed something.

Everyone knows that one pass data to a page in an anchor tag by using the GET method:

What I am wondering is if there was a way to do the same thing, but use the POST Method instead?

My purpose in doing so is to keep the URLs the user sees clean by not putting anything in them that they do not need to see.

This has nothing to do with security concerns as I already know there would be ways to obtain the data being passed.

If the answer is indeed no, then what methods do people use to pass data when there is a desire to keep the URLs clean? Cookies? Something else?

and how to deal with the scenarios when the URL length exceeds the permissible GET request length

I am facing this issue while implementing sorting/pagination with displaytag, all the request parameters are appending in the sort/pagination url which is more then the permissible length of the GET request.

like image 867
dpsdce Avatar asked Dec 06 '11 10:12

dpsdce


People also ask

Can we use POST method in anchor tag?

This behaviour is specific to display tag library. It allows for easily bookmarkable search results. If you really intend to change this to make use of POST, then you'd need to rewrite the display tag library or bring in some jQuery to manipulate the links. The remnant of your questions boils nowhere.

How do you tag a POST in HTML?

There is no way to POST an a element using only HTML. There is no attribute that controls whether to use POST or GET with an a element. You have to script it, if you want to abuse the semantics.

How can I submit a POST form using the A HREF tag?

You can use href=”#top” or href=”#” to link to the top of the current page. To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function.

Can we pass parameters in anchor tag like using POST method?

The only way to pass data with an anchor tag is to put them in the query string.


5 Answers

You could do something like this:

<form method="post" action="target.html">
  <input type="hidden" name="name" value="value" /> 
  <a onclick="this.parentNode.submit();">click here</a>
</form>
like image 70
loscuropresagio Avatar answered Oct 03 '22 21:10

loscuropresagio


This behaviour is specific to display tag library. It allows for easily bookmarkable search results. If you really intend to change this to make use of POST, then you'd need to rewrite the display tag library or bring in some jQuery to manipulate the links.

The remnant of your questions boils nowhere. If you want GET (idempotent requests, bookmarkable URLs, searchbot-crawable URLs, etc), then use GET. If you want POST (non-idempotent requests, non-bookmarkable URLs, non-crawlable URLs, etc), then use POST.

Usually, POST is mandatory when the request can modify the data in the server. Think of a SQL INSERT, UPDATE, DELETE, etc. You certainly won't make this kind of requests GET. Imagine that you've a table with all "delete row" links which do GET and then a searchbot comes along...

like image 21
BalusC Avatar answered Oct 03 '22 23:10

BalusC


You can use javascript. On onclick of link do form.submit

The only way I know of to deal with lenghty URL is to instead use POST.

like image 34
PrasadB Avatar answered Oct 03 '22 21:10

PrasadB


You may create a temporary form and submit it while onclick event of <a> tag.

like image 32
Selvakumar Ponnusamy Avatar answered Oct 03 '22 22:10

Selvakumar Ponnusamy


It will work as post ,the name value can be through anchor tag and value of name="" can be access to $_POST[] globl var

like image 26
sunil jaiswal Avatar answered Oct 03 '22 23:10

sunil jaiswal