form method=link or a? What's the difference?
That page you link to is incorrect. There is no link
value for the method
attribute in HTML. This will cause the form to fall back to the default value for the method attribute, get
, which is equivalent to an anchor element with a href
attribute anyway, as both will result in a HTTP GET
request. The only valid values of a form's method
in HTML5 are "get" and "post".
<form method="get" action="foo.html">
<input type="submit">
</form>
This is the same as your example, but valid; and is equivalent to:
<a href="foo.html">
You should use semantics to determine which way to implement your form. Since there are no form fields for the user to fill in, this isn't really a form, and thus you need not use <form>
to get the effect.
An example of when to use a GET
form is a search box:
<form action="/search">
<input type="search" name="q" placeholder="Search" value="dog">
<button type="submit">Search</button>
</form>
The above allows the visitor to input their own search query, whereas this anchor element does not:
<a href="/search?q=dog">Search for "dog"</a>
Yet both will go to the same page when submitted/clicked (assuming the user doesn't change the text field in the first
As an aside, I use the following CSS to get links that look like buttons:
button,
.buttons a {
cursor: pointer;
font-size: 9.75pt; /* maximum size in WebKit to get native look buttons without using zoom */
-moz-user-select: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: transparent;
}
.buttons a {
margin: 2px;
padding: 3px 6px 3px;
border: 2px outset buttonface;
background-color: buttonface;
color: buttontext;
text-align: center;
text-decoration: none;
-webkit-appearance: button;
}
button img,
.buttons a img {
-webkit-user-drag: none;
-ms-user-drag: none;
}
.buttons form {
display: inline;
display: inline-block;
}
Should PUT and DELETE be used in forms?
Your question involves two closely related but separate standards, HTTP and HTML. The PUT and DELETE methods are part of HTTP. In HTTP they have obvious use in RESTful interfaces, and other services which build on HTTP such as Webdav.
HTML up to version 4 only defines the use of POST and GET for forms. HTML5 at this time appears as though it may support the further methods. [note, support is not included in the current w3 draft]
Any current browser support (I'm not directly aware of any) will be very limited and only really useful as an experiment at the bleeding edge.
Difference between input type='button' / and input type='submit' /
<input type="button" />
buttons will not submit a form - they don't do anything by default. They're generally used in conjunction with JavaScript as part of an AJAX application.
<input type="submit">
buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.
The first submit button of the form is also the one being clicked for implicit submission, f.e. by pressing enter in a text input.
Is there any difference between parameters in a URL and form method=get?
The HTTP spec does not set limitations, but the browsers and servers do. See here for specifics.
The browser will create a long URL if the method is set to GET for a form, so the above limitations apply.
HTML form with multiple actions
As @AliK mentioned, this can be done easily by looking at the value of the submit buttons.
When you submit a form, unset variables will evaluate false. If you set both submit buttons to be part of the same form, you can just check and see which button has been set.
HTML:
<form action="handle_user.php" method="POST" />
<input type="submit" value="Save" name="save" />
<input type="submit" value="Submit for Approval" name="approve" />
</form>
PHP
if($_POST["save"]) {
//User hit the save button, handle accordingly
}
//You can do an else, but I prefer a separate statement
if($_POST["approve"]) {
//User hit the Submit for Approval button, handle accordingly
}
EDIT
If you'd rather not change your PHP setup, try this: http://pastebin.com/j0GUF7MV
This is the JavaScript method @AliK was reffering to.
Related:
- 2x submit buttons to action different URL
- Submit form to another page (which is different from the page used in ACTION)
Can a HTML button perform a POST request?
This can be done with an input element of a type "submit". This will appear as a button to the user and clicking the button will send the form.
<form action="" method="post">
<input type="submit" name="upvote" value="Upvote" />
</form>
Submit form using a button outside the form tag
Update: In modern browsers you can use the form
attribute to do this.
As far as I know, you cannot do this without javascript.
Here's what the spec says
The elements used to create controls generally appear inside a FORM
element, but may also appear outside of a FORM element declaration
when they are used to build user interfaces. This is discussed in the
section on intrinsic events. Note that controls outside a form cannot
be successful controls.
That's my bold
A submit
button is considered a control.
http://www.w3.org/TR/html4/interact/forms.html#h-17.2.1
From the comments
I have a multi tabbed settings area with a button to update all, due
to the design of it the button would be outside of the form.
Why not place the input
inside the form, but use CSS to position it elsewhere on the page?
what do form action=# and form method=post action=# do?
Action normally specifies the file/page that the form is submitted to (using the method described in the method paramater (post, get etc.))
An action of #
indicates that the form stays on the same page, simply suffixing the url with a #
. Similar use occurs in anchors. <a href=#">Link</a>
for example, will stay on the same page.
Thus, the form is submitted to the same page, which then processes the data etc.
button vs. input type=button /. Which to use?
- Here's a page describing the differences (basically you can put html into a
<button></button>
) - And another page describing why people avoid
<button></button>
(Hint: IE6)
Another IE problem when using <button />
:
And while we're talking about IE, it's
got a couple of bugs related to the
width of buttons. It'll mysteriously
add extra padding when you're trying
to add styles, meaning you have to add
a tiny hack to get things under
control.
Related Topics
Xpath to Select Between Two HTML Comments Is Not Working
Linethrough/Strikethrough a Whole HTML Table Row
Negative Margin and Background
How to Center Cards in Bootstrap 4
Preserve Line Breaks in Textarea
How to Tell Browsers to Honor The Jpeg Exif Orientation
Getting Youtube.Com to Load in Iframe
Best Way to Implement Background Image on HTML or Body
When Printing Tables in Google Chrome, Content Overlaps Header
CSS Border Color Switch Animation: "From" Color Not Correct
Difference Between Svg and HTML5 Canvas
Preventing Canvas Clear When Resizing Window
How to Make a Div with Arrowlike Side Without CSS Border Tricks
What Happens If The Action Field in a <Form> Has Parameters