giles
giles

Reputation: 39

submitting a hyperlink by GET or POST

So there's this hyperlink - it's happy being a hyperlink - it does not want to change to a button or a form element - it wants to stay a link!

But it would really help me if I could submit it via GET or POST (something which I switch on my pages due to design criteria). Is there ANY way that I can do this

thanks Giles

Upvotes: 3

Views: 7611

Answers (4)

Aryan G
Aryan G

Reputation: 1301

As you do not want to go for form elements, there is no need of using POST method. Simply, what you can do is:

  <a href="page_location?foo=bar">Link</a>

And in the page_location page,

<?php
    $foo = $_GET['foo'];  //here, assigns $foo = bar
    // required actions
?>

Hope you get the solution! :)

Upvotes: 1

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

You're in luck... clicking a hyperlink already does a GET request.

If you want to add query parameters, append them to the query string like so:

<a href="/my/page/foo.php?onions=no&pickles=yes">link text</a>

Upvotes: 4

Sudhir Jonathan
Sudhir Jonathan

Reputation: 17516

I'll assume you're not really using the link as a link... that you want to submit a form with it. You'll want to keep the link inside the form you want to submit, and you can do this (jQuery):

$('#link').parents('form').attr('method', 'GET').submit();

or

$('#link').parents('form').attr('method', 'POST').submit();

Upvotes: 0

casablanca
casablanca

Reputation: 70701

Assuming you already have a form that you want to submit, you can use JavaScript to make the link submit the form:

<form id="myform">
  ...
</form>

<a href="#" onclick="document.getElementById('myform').submit();">Submit</a>

Upvotes: 3

Related Questions