OrangeRind
OrangeRind

Reputation: 4818

Posting data through hyperlinks

I want to post ($_GET as well as $_POST) data by clicking on links (enclosed with <a>) and not the regular form 'submit' button. preferred language : PHP

I have seen this in a lot of todays websites, where forms are submitted by clicking on buttons looking like hyperlinks. so was wondering how it could be done.

Thanks in advance

Upvotes: 6

Views: 9902

Answers (6)

Quentin
Quentin

Reputation: 943214

Forms are designed to be submitted with buttons. You can use JavaScript to make a link submit it, but this will break when JS is not available. Even if JavaScript is available, using a link will be using a control which won't show up when a screen reader is in "Forms Mode", leaving screen reader users without any obvious way to submit the form.

CSS is a safer alternative (see http://tom.me.uk/scripting/submit.html).

Upvotes: 7

l0b0
l0b0

Reputation: 58788

As seen from the other answers, you have to use JavaScript if you want to submit user data from a form with a link. That means you exclude anyone with JavaScript turned off, unavailable, or incompatible with your code. On the other hand, you could try to style a button with CSS to make it look like a link.

Upvotes: 2

Dan F
Dan F

Reputation: 12052

This post on Ajaxian might help. It links to a pretty in depth blog post that shows you how to apply css to buttons so that they look like links.

The advantage here over using a proper link is the "fake link" really is a button, so it behaves exactly like a button, only it looks like a link. Spiders won't follow it, screen readers will treat it differently, it's a more "correct" thing to do in terms of launching a http post.

Upvotes: 12

Jeremy Smyth
Jeremy Smyth

Reputation: 23493

You could do something like:

<a href='myform.php?name=Bob&surname=Terrier'>Click here</a>

That will submit name and surname as GET variables to the form.

Beware any form that modifies data though, as search engines and spiders will traverse links where they don't traverse forms.

Upvotes: 2

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74202

You can make use of Javascript to submit the form.

<a href='document.myform.submit()'>Submit Button</a>

Upvotes: 0

Matt Lacey
Matt Lacey

Reputation: 65564

Beware posting data by clicking on links! Spiders may follow this links and change data.

You probably want something like:

<a href="#" onclick="form.Submit();">submit</a>

Upvotes: 1

Related Questions