views:

586

answers:

7

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

A: 

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>
Matt Lacey
As fallbacks for when JS is not available go, "A link to the top of the page (#)" is pretty poor.
David Dorward
a good spider won't submit a form if it's method is set to "post"
Adrian Mester
A: 

You can make use of Javascript to submit the form.

<a href='document.myform.submit()'>Submit Button</a>
Alan Haggai Alavi
As URLs go, that is a normal relative URL (but quite an ugly one)
David Dorward
i don't know Javascript that well as to submit data.may be you could specify the code for submit()
OrangeRind
+2  A: 

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.

Jeremy Smyth
+1 for mentioning the "GET" method while submitting data via URLs.
TFM
OrangeRind
@OrangeRind: You should then update your original question, there's no mention of "POST" in there.
TFM
@TFM Umm. Word number 4 of the question?
David Dorward
"I want to post data" was a bit of a giveaway :-)
Dan F
Not really. Post can easily be interpreted as "submitting" data, which is what happens even when the "GET" method is used.
TFM
no... POST is POST. End of story.
Charlie Somerville
Not according to the question: "I want to post ($_GET as well as $_POST) data"... ;)
TFM
Parenthesised bit was edited after the answer :)
Jeremy Smyth
+7  A: 

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).

David Dorward
Even in light of our little fit - your answer still gets my vote ;)
Jonathan Sampson
thanks a lot! really helped!
OrangeRind
@Jonathan Thanks :)
David Dorward
+6  A: 

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.

Dan F
'fake' eh? ;-)thanks
OrangeRind
No worries. This turned out to be quite a hot little question :-)
Dan F
might consider voting it up. ;)
OrangeRind
+2  A: 

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.

l0b0
A: 

Hey Buddy, judging by

preferred language : PHP

I guess, that you're new to the world of web programming.

And

clicking on buttons looking like hyperlinks

If you want to make buttons look like hyperlinks, you may just change the cursor from pointer to hand. Unless you look at the source code, your button is like a hyperlink!

<input type="submit" src="someimage" style="cursor: hand;" />

This will be sufficient if you want to make it look like a hyperlink.

CodingTales
Actually... cursor: pointer is better. cursor: hand is a very old IE specific style that doesn't work anywhere outside IE and Opera. See http://www.quirksmode.org/css/cursor.html for more details
Dan F
And there is rather more difference between the default rendering of a link and a button than the cursor used.
David Dorward
And using php doesn't make him "new to the world of web programming"
Blindy