Coding with Jesse

Use an empty action to submit a form to the current page

November 3rd, 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>

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.

Interested in web development? Subscribe to my newsletter!


1 . Michael at 2007-11-03T20:10:43.000Z


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 2007-11-03T20:17:37.000Z

Jesse Skinner

@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 2007-11-05T09:17:36.000Z


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 2007-11-05T09:24:15.000Z

Jesse Skinner

@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 2007-11-23T03:10:36.000Z

Matthew James Taylor

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 2007-11-23T15:53:05.000Z


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.


7 . Jesse Skinner at 2007-11-23T15:58:26.000Z

Jesse Skinner

@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 2007-11-23T16:10:34.000Z


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 2007-11-23T16:13:25.000Z

Jesse Skinner

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:

I'm not exactly sure how the 2 comment spams a week get through, maybe they're not bots?

10 . Jesse Skinner at 2007-11-23T16:16:12.000Z

Jesse Skinner

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 2007-12-06T10:45:03.000Z


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

12 . Sean at 2008-09-16T00:15:54.000Z


I am new to JavaScript. I am trying to add comments feature to my web site. I have added the form action code to my web page but it still doesn't work. Could you tell me what else should I do? Thanks.

13 . Jesse Skinner at 2008-09-16T00:42:48.000Z

Jesse Skinner

@Sean - depends on how much code you have there already. If you mean you just copied the HTML code over, you'll need to have server-side code using PHP, ASP, Ruby, etc. which takes the form data, adds it to some database, and then later retrieves the comments and displays them on the page.

If you already have all this in place and are still having trouble, feel free to post a link to the page here and maybe someone can help you out.

14 . Sean at 2008-09-17T15:35:59.000Z


Here is my web page -

What would a corresponding PHP file look like? Thanks

15 . Sean at 2008-09-17T23:15:22.000Z


All I want is to allow visitors to add comments to my web page. Do I need MySQL? All messages can be saved right on the web page. Right? Does this page use MySQL? Thanks

16 . Jesse Skinner at 2008-09-18T02:40:43.000Z

Jesse Skinner

@Sean - yes, this site uses a MySQL database to store comments and blog posts. They are not stored in the web page itself. The web page as you see it is created from the contents of the database every time it loads.

It would be out of the scope of a comment (perhaps even a blog article) to explain the code needed to implement comments. I suggest searching the web for PHP blog comment examples and tutorials, or consider using Wordpress or another blog package to handle this stuff for you.

17 . Stephen R at 2009-02-04T18:18:35.000Z

Stephen R

So what happens if you do this from a page such as index.php?x=123

Does it return to index.php?x=123 or just the base index.php ?

18 . Jesse Skinner at 2009-02-04T18:20:14.000Z

Jesse Skinner

@Stephen R - I'm not sure - maybe you could try it out in the different browsers and let us know! It'd be really interesting to see how this plays together with method="GET".

19 . Frank Weindel at 2009-02-20T20:25:59.000Z

Frank Weindel

At a website I'm working for we use the <base> tag to set the base URL for the entire site to our domain root. We have a form which uses a blank action attribute and on the versions of Firefox, IE, and Safari I've tested, it points correctly to the current page. However I had a user report that the form was taking him to our domain root, leading me to believe that some browsers/versions might be interpreting it as a relative URI and tacking the base URL in front of it. He said he was using Safari. I'm still in correspondence with him to find out what version it is.

20 . Frank Weindel at 2009-02-21T01:00:10.000Z

Frank Weindel

It was version 3.2.1 on Mac. I tested the latest Safari for Windows version 3.2.2 and I experienced the same problem.

21 . JoeCoder at 2009-03-09T15:14:17.000Z


Here is another way to submit a form to the current page:

<form method="post">

In other words, don't even mention action.

Or you can simply do this (which submits the form to the current page and defaults to method="get"):


22 . Andy at 2009-04-01T09:49:21.000Z


well .. try that in GoogleChrome .. the action attribute is required and must not be empty ! In chrome empty action means root domain page and not current page !

23 . Art at 2009-05-18T04:21:50.000Z


WARNING - If your page contains a <base> tag, using action="" will crash in Google Chrome. Leaving the "action" attribute out of the <form> tag altogether will prevent you html from validating as the "action" attribute is required for the <form> tag.

24 . web development at 2009-06-13T07:56:30.000Z

web development

I am trying to add comments feature to my web site.

thanks for this.

25 . ☨ at 2009-06-13T12:02:53.000Z


@JoeCoder: Action is required attribute in commonly used standards.
@Frank: If you read the RFC's section 4.2. carefully, it states that if base is defined, empty URI in action should point to it.

26 . holyhttp at 2009-08-15T15:24:01.000Z


Using an empty "action" attribute in a form reduces form portability. Just because an empty form's "action" attribute works does not mean we have to become sloppy and leave it out altogether.

Let's just for example imagine a subscription form you would like to display on each page throughout your entire web site. In such as case if you started out with an empty action attribute your form will only work on the original page it was meant to be placed in.

You will have to go to some retweaking to make it work on other pages. Adopting those bad habits can become a second nature that would be costly in the long run.

Standards are set for a valid reasons and we should all do our best to subscrive to them.

Thanks for this post though Jesse.

27 . Kawika at 2009-11-12T20:18:54.000Z


Great post. I encountered an empty action attribute in the WordPress MU code and didn't know what it meant. Googling about it brought me to this post! Thanks for the great post!

28 . python at 2009-12-09T19:29:45.000Z


What does action="." means?

29 . Optio at 2009-12-10T13:18:01.000Z


@python: It means submit the form to the default page of the current directory.

30 . James Gray at 2009-12-14T05:25:33.000Z

James Gray

Thank you for the info on a empty attribute.
I was doing a g search to look for optional input type values ''text'' that could be used for the input type. As i study PHP knowing it is a loosely typed language i figure the html would have some options for a input type.
any way back at the ranch with the empty action attribute, i saw a attribute of color for a input type, i think there was also a option for text as well, would that lead anybody to offer a change of text color and font aside from J Script or PHP.

31 . Jesse Skinner at 2009-12-14T17:02:47.000Z

Jesse Skinner

@James - I believe you're talking about one of the new input types in HTML 5:

You're right, the idea is that the browser will have a built-in color picker that works without JavaScript. I don't think any browsers support this yet, though.

32 . Peter Brand at 2010-05-13T04:54:28.000Z

Peter Brand

This is an old article, but I thought it interesting to post a side effect of leaving the action blank on a form.

In Classic ASP if the URL of the page contained a querystring, then that will be built into the action. The result is that BOTH the Request.QueryString AND Request.Form collections will contain data.

E.G Say you navigate to a page with the URL "" and then on that page have a form with action blank containing input boxes with names of "a" and "b". When the form is submitted; Request.Form("a"), Request.Form("b"), Request.Querystring("a") will ALL contain data. Request("a") will probably contain an array.

I haven't tested this with other web servers.

33 . Montreal website design company at 2010-09-02T19:11:14.000Z

Montreal website design company

I am fully agree with Peter. This post should be redefined

34 . christine joseph at 2010-10-26T14:38:39.000Z

christine joseph

You have some nice points here. I done a research on the topic and got most people will agree with your blog.

35 . free seo report at 2011-03-07T03:35:16.000Z

free seo report

Thanks. I just thought there its a kind of trick leaving action=""
Many Thanks.

36 . zoe somebody at 2011-05-11T19:23:27.000Z

zoe somebody

Though HTML5 continues to permit (supposedly) the empty action attribute, in the HTML5 spec leaving it out entirely to indicate the form should post to the current page is acceptable and indeed is the only way I could validate a form.

form action="" (wouldn't validate for HTML5 today)
form (would validate with the action omitted entirely)

Hope that helps somebody.

37 . Dan at 2011-08-12T21:10:14.000Z


Where do I find the submission if I leave ation=""?
You say it goes on the same page but where do I physically find it?

38 . Evgeniy Dolzhenko at 2011-11-28T10:25:49.000Z

Evgeniy Dolzhenko

Just what I was looking for, thanks!

Would like to learn more about HTML5 way, should the action really be omitted to be valid HTML5?

39 . sunny at 2012-01-05T10:00:55.000Z


hi i am sunny i want to post form data to same page & save on the page but when i recreate last entry get deleted .kindly help me i will be thankfulto you

40 . sunny at 2012-01-05T10:01:58.000Z


hi i am sunny i want to post form data to same page & save on the page but when i recreate last entry get deleted .kindly help me i will be thankfulto you. i posted <html>
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.


41 . Ged at 2012-02-12T11:22:11.000Z


Sunny if I'm reading you right you're not saving the values anywhere. Each time you execute the form the form is wiped clean - all variables will be overwritten with new values. If you want to keep them you'll need to save them somewhere else, usually a database.

Lovely little post Jesse :-) short, clear and exactly what I needed.

42 . Ged at 2012-02-12T11:26:57.000Z


"The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces."

so the empty string trick does break html5. Joy >.<

43 . mayank agarwal at 2012-04-13T03:56:30.000Z

mayank agarwal

what does form action="#" do>

44 . Srinivas at 2012-08-20T12:48:43.000Z


if i give empty space action="" .In ie 8 it not getting cuurent url.
but if i use
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">

am getting current url.
Thank u