Post from an <A> Tag

POST from an A tag

There is no way to POST an a element using only HTML.

As can be seen from this DTD fragment (HTML 4.01 spec):

<!ELEMENT A - - (%inline;)* -(A)       -- anchor -->
<!ATTLIST A
%attrs; -- %coreattrs, %i18n, %events --
charset %Charset; #IMPLIED -- char encoding of linked resource --
type %ContentType; #IMPLIED -- advisory content type --
name CDATA #IMPLIED -- named link end --
href %URI; #IMPLIED -- URI for linked resource --
hreflang %LanguageCode; #IMPLIED -- language code --
rel %LinkTypes; #IMPLIED -- forward link types --
rev %LinkTypes; #IMPLIED -- reverse link types --
accesskey %Character; #IMPLIED -- accessibility key character --
shape %Shape; rect -- for use with client-side image maps --
coords %Coords; #IMPLIED -- for use with client-side image maps --
tabindex NUMBER #IMPLIED -- position in tabbing order --
onfocus %Script; #IMPLIED -- the element got the focus --
onblur %Script; #IMPLIED -- the element lost the focus --
>

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.

Using the POST Method with HTML Anchor Tags

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>

How to write a POST method to an a tag in Django

a tags back to the time the web was literally a web with pages linked to each other by hyperlinks. POST is an HTTP concept and when it comes to HTML it is only possible by using "form submission".

When you are using an HTML form, you make the browser always send the request to the same URL (i.e. action). When a browser opens a page containing a form there are several ways for the form to be submitted:

  1. In case the form contains at least an input element with type equal to 'submit', when the user clicks on the submit button the form gets submitted and the request is sent to the server,
  2. User press enter in existing input elements within the form which again causes the form to be submitted.

The method attribute in the form element only affects the way browser works when a form is submitted. a elements do not interact with their surrounding form and the a element inside the form works just like an a element outside the form. The a element you've put inside the form only causes the browser to navigate to its href regardless of the containing form or its method attribute.

Now to answer your question

Is this the best way to use a Post method?

The correct way of using a post method is any of these ways:

  1. By having a form with its method set to POST and with some input elements inside it which contain data or user input,
  2. By doing javascript, in your case maybe on the onclick action of you a elements, to make specific POST requests. You can find a canonical guide here.
  3. Or as WorkShoft said, set a name attribute on the form, and call document.[name].submit() when the link is clicked.

how to use post method instead get for anchor tag

your best option is to use a form with a submit button that looks like a link.

It's impossible to use an anchor tag to send a post request. If using an anchor is a requirement then you need some javascript code.

Making href (anchor tag) request POST instead of GET?

Using jQuery it is very simple, assuming the URL you wish to post to is on the same server or has implemented CORS

$(function() {
$("#employeeLink").on("click",function(e) {
e.preventDefault(); // cancel the link itself
$.post(this.href,function(data) {
$("#someContainer").html(data);
});
});
});

If you insist on using frames which I strongly discourage, have a form and submit it with the link

<form action="employee.action" method="post" target="myFrame" id="myForm"></form>

and use (in plain JS)

 window.addEventListener("load",function() {
document.getElementById("employeeLink").addEventListener("click",function(e) {
e.preventDefault(); // cancel the link
document.getElementById("myForm").submit(); // but make sure nothing has name or ID="submit"
});
});

Without a form we need to make one

 window.addEventListener("load",function() {
document.getElementById("employeeLink").addEventListener("click",function(e) {
e.preventDefault(); // cancel the actual link
var myForm = document.createElement("form");
myForm.action=this.href;// the href of the link
myForm.target="myFrame";
myForm.method="POST";
myForm.submit();
});
});

is a tag post method or get method?

Get.
Opening a url is a get method, i think the same apply for a redirect.

send $_POST data via anchor tag

You can achieve this using jQuery and a HTML form

HTML:

<form method="post" name="redirect" class="redirect">
<input type="hidden" class="post" name="post" value="">
<input type="submit" style="display: none;">
</form>

Button: (html)

<a href='javascript:void(0)' class='button' var='DATAHERE'>sometexthere</a>

Javascript, or rather said jQuery:

$(".button").click(function() {
var link = $(this).attr('var');
$('.post').attr("value",link);
$('.redirect').submit();
});

this jQuery code listen's to any clicks on the items with the class button attached to them,
and reads out their "var" value, basicly you could use any kind of HTML element using this method as long as they have the button class attached to it.

How to submit a POST using a href= tag?

you can use get method instead for sending data for eg.
"form.php?name1=value1&name2=value2"

<?php
$myfile = fopen("pictures.txt", "r") or die("Unable to open file!");
while(!feof($myfile)){
$pic_name= fgets($myfile);
echo '<div class="grid-item">';
echo '<a href="redirect.php/?pic_name=".$pic_name.><img src="' . $pic_name . '.jpg"></a>';
echo '</div>';
}
?>


Related Topics



Leave a reply



Submit