the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Use an empty action to submit a form to the current page

    Nov 3 2007

    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.

    Tags: html form uri rfc
    View 11 Comments | Add a comment
  • Comments

    1. Michael at 10:10pm on November 3, 2007

    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?

    2. Jesse Skinner at 10:17pm on November 3, 2007

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

    3. Dave at 10:17am on November 5, 2007

    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.

    4. Jesse Skinner at 10:24am on November 5, 2007

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

    5. Matthew James Taylor at 4:10am on November 23, 2007

    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!

    6. Michael at 4:53pm on November 23, 2007

    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

    7. Jesse Skinner at 4:58pm on November 23, 2007

    @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 :)

    8. Michael at 5:10pm on November 23, 2007

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

    9. Jesse Skinner at 5:13pm on November 23, 2007

    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?

    10. Jesse Skinner at 5:16pm on November 23, 2007

    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.

    11. sandeep at 11:45am on December 6, 2007

    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

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • JavaScript Functions are Variables
    • See All...
  • Categories

    • javascript (37)
    • links (19)
    • about (18)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites