The title says it all: you can use an empty action attribute on a form to submit that form to the current page. This means you don't need to use server-side scripting (using REQUEST_URI or PHP_SELF or whatnot) to write the current URL into the HTML.
The following is perfectly valid:
<form action="" method="post">
<p><input type="submit"/></p>
</form>
Now beware, the action attribute is mandatory, and it must contain a valid URI. But according to the URI RFC, an empty URI is still a URI:
4.2. Same-document References
A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document. Traversal of such a reference should not result in an additional retrieval action. However, if the URI reference occurs in a context that is always intended to result in a new request, as in the case of HTML's FORM element, then an empty URI reference represents the base URI of the current document and should be replaced by that URI when transformed into a request.
So there you have it. Enjoy.
Thanks for this tip. I think. It definitely makes sense.
Can you further illuminate a noob with an example of how this instance might be put into play?
@Michael - well let's say you have a comments form on a blog post template, and you want the form to submit to the blog post's URL. You would either have to dynamically put the URL into the form like this (in PHP):
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
or you could just do this:
<form action="" method="post">
Of course it's not a big deal to stick the URL in. But it's nice to know there's a short cut too.
This is very cool. Do you know if all browsers support it? What about form action="#section"?
Also, if you are currently browsing index.html#section and submit a form then will the form get posted to the anchor?
Not sure if it would be useful, but it's interesting to know.
@Dave - all the major browsers support it, but with so many out there it's very possible that this might trigger a bug in some browsers, and not submit or submit to the wrong page.
With an anchor like action="#section" - this worked as expected in Firefox. It sort-of worked in Internet Explorer too -- the browser scrolled down to the anchor section, although the actual #section anchor was dropped from the URL if method="get"!
I wonder what other browsers would do...
This is excellent! - I just tested it on rewritten URLs using Mod Rewrite and it works there too!
Thanks for the tip Jesse, this is a great blog!
Jesse, just a quick comment/observation.
I'm not sure if this is an wordPress bug or not. I subscribed to this feed at my first comment above, and twice now i've received email updates on this post that are obviously spam related.
Interestingly enough, when i come here to this post, those 'comments' of course are not to be seen (thank goodness).
Is this a case where people/bot has commented, and you've deleted comment, but only after the app has already sent out the notice to subscribers?
Just thought I'd let you know.
Michael
@Michael - yeah, those were spam comments posted to this page, and I deleted them as soon as I saw them. Sorry you had to see them too.. I only get about 2 spam comments a week so I guess you were just really unlucky. I guess there's no proper solution except to unsubscribe from notifications.
BTW, I'm not running WordPress, I'm running handmade blog software :)
Thanks Jesse.
and sorry for the w/p dis. I think on another post you had mentioned the fact this blog was hand-tooled.
Is your 'commenting' section completely hand-tooled as well?
Given your prowess, perhaps the inclusion of some sort of captcha-like 'comment protection' exercise might make a great tutorial-post for FOW. I'd be happy to volunteer artwork for it.
Thanks again for your blog. :)
Yeah I made the comments part too. And there is actually a bit of a CAPTCHA using JavaScript (when JS is enabled, the CAPTCHA is auto-filled). I wrote about it here:
http://www.thefutureoftheweb.com/blog/avoiding-comment-spam-with-javascript
I'm not exactly sure how the 2 comment spams a week get through, maybe they're not bots?
Oops, just realised that I'd written only about my older technique that simply required JavaScript. I've since replaced it with a simple CAPTCHA instead. Disable JavaScript to see what I mean.
hi,
how to post forms data to an email address or to the web server FTP with out using either client-side scripting or server-side scripting.plzzz help me in this issue