-3

Is it possible to make a form submit POST style with an anchor?

<form>
    <input name="foo" />
    <input name="bar" />

    <a href="#">Submit this form</a>
</form>

Original question, also gets explained in this answer by Martijn

Is it possible to pass a value using hyperlink as a POST method ?? i want to pass a value on clicking an image to the next page.

Community
  • 1
  • 1
Aayush
  • 223
  • 1
  • 3
  • 13
  • [en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods) might be a good read. – Amal Murali Sep 30 '13 at 12:38
  • You could do an Ajax call to override the link. – Sébastien Sep 30 '13 at 12:40
  • 1
    Clicking on a link that just has a reference to another URL will fire a `GET` request. If you want to make a post, you'll need to use Javascript to force a `POST` on the element's click event. Cross domain issues may happen if you're linking to another domain. – Geeky Guy Sep 30 '13 at 12:40
  • 3
    Whilst no code has been posted, this is not a stupid question if you're a newbie. One might well wonder, if you're new to all this, why this is possible only via a form or AJAX, rather than via a hyperlink. – Mitya Sep 30 '13 at 12:40
  • was there any thing wrong :o – Aayush Sep 30 '13 at 12:41
  • @Aayush I'm nominating this questions for reopening. So far you've got 2 votes out of a necessary 5 to reopen. Do read the closure message below to get a better idea of how to avoid having downvotes and closed questions. I almost agree with the guys that closed your question. – Geeky Guy Sep 30 '13 at 12:42
  • a form is obviously included.I thought people would understand ...my bad – Aayush Sep 30 '13 at 12:43
  • 1
    I've editted your anwer to include a form so your question gets more obvious – Martijn Sep 30 '13 at 13:08

1 Answers1

1

Im going to leave my original answer below, why an anchor can not perform a post call. However, the question has been rephrazed, this answer will answer that question:

You can not use a anchor to submit, but its somewhat common practice to make it trigger a submit:

$('a#myAnchor').click(function(){ $('#myForm').submit() })
/// Or an image, doesn't really matter for jQuery:
$('img#myImg').click(function(){ $('#myForm').submit() })
// Or native js:
document.getElementById('myImg').onclick = function(){ document.getElemetById('myForm').submit() });

if the element you want to trigger a submit with is inside of a form:

$('element').click(function(){ $(this).closest('form').submit() });

No. Anchors are GETbecause anchors send you to an url, which makes it GET, never POST.

POST is data being send in post-format, it gets encoding in a way the server can handle it (important for images and such)

In this other topic this reply explains difference between GET and POST

If you must, and I mean REALLY must, you could use jQuery to fake a post, something allong these lines:

// THIS IS AN UNTESTED DRAFT! Just to shown you a direction:
$('a').click(function(e){
    e.preventDefault();
    var scriptname = this.href.replace(/\??(.*)?/, ''); // remove everything after '?'
    var query     = this.href.replace(/(.*)\?/, ''); // remove everything before '?'
    $.post(scriptname, query);
});

But again, DONT DO THIS, unless you really REALLY have to. This will send a ajaxcall to the server, so the page wont actually change. Because this is bad practise, im not gonna show you how to make the page change on completion, you'll have to figure this out with the jQuery POST documentation

Community
  • 1
  • 1
Martijn
  • 15,791
  • 4
  • 36
  • 68