Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit button in a hyperlink

Tags:

html

forms

I would have never thought that it was valid markup to put a <input type="submit" /> in an anchor <a> like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Submit input in a hyperlink</title>
</head>
<body>
    <form action="/form-submission" method="get">
        <a href="/hyperlink-click"><input type="submit" /></a>
    </form>
</body>
</html>

Oddly enough though, if you feed this into the w3c validator, it validates.

What is supposed to happen when a user clicks on the submit button? Would this kind of setup just be unpredictable with different browsers doing whatever they please, or are there explicit rules dictating expected behavior? Would doing something like this ever come in handy?

like image 923
Richard JP Le Guen Avatar asked May 31 '26 22:05

Richard JP Le Guen


1 Answers

The behaviour isn't consistent between browsers.

Milimetric answer may be good for javascript, but consider what happens with unscripted html.

See the js fiddle http://jsfiddle.net/PjtPZ/

Here I've just changed the html of the question so that the link navigates the page to www.microsoft.com and the form submits to www.google.com.

Click on the button in Firefox 4.0, IE9 or Opera 11 and the Google page is the one that appears. Do it in Chrome 10.0 or Safari 4.0 and you get the Microsoft page.

This is probably why it is disallowed in HTML5.

like image 57
Alohci Avatar answered Jun 03 '26 13:06

Alohci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!