151

I want an HTML form to submit to itself. How do I use the action attribute?

  1. <form action="">
  2. <form action="#">
  3. <form action="some/address">
  4. <form>

Which was is preferable?

3

5 Answers 5

179

In 2013, with all the HTML5 stuff, you can just omit the 'action' attribute to self-submit a form

<form>

Actually, the Form Submission subsection of the current HTML5 draft does not allow action="" (empty attribute). It is against the specification.

From this other Stack Overflow answer.

4
  • 1
    I used <?php $_PHP_SELF ?> and got the error that action cannot be blank, i guess ill just leave it off
    – Drewdin
    Commented Feb 18, 2015 at 12:04
  • 4
    I'm confused. Is it w3 valid to leave off the action attribute all-together, but not to leave it empty? Commented May 22, 2017 at 5:07
  • To I need to access the parameters with $_GET or $_POST then?
    – Black
    Commented May 23, 2019 at 12:51
  • @Black This would obviously be specified in the method-tag
    – clockw0rk
    Commented Mar 12, 2021 at 10:06
31

Use ?:

<form action="?" method="post">

It will send the user back to the same page.

5
  • This is awesome!
    – beppe9000
    Commented Jan 20, 2019 at 15:16
  • 16
    Not so awesome. This would remove all query strings of the current URL if it has them.
    – OMA
    Commented Aug 30, 2019 at 17:13
  • 4
    I want to remove the query strings :3
    – VoidZA
    Commented Oct 10, 2019 at 14:04
  • @OMA doesn't form submit already remove all query strings? Commented Feb 19 at 21:03
  • No, it doesn't remove query strings. Form submit will send form values through POST and query strings in the address through GET, like any other URL.
    – OMA
    Commented May 2 at 4:27
28

You can leave action attribute blank. The form will automatically submit itself in the same page.

<form action="">

According to the w3c specification, action attribute must be non-empty valid url in general. There is also an explanation for some situations in which the action attribute may be left empty.

The action of an element is the value of the element’s formaction attribute, if the element is a Submit Button and has such an attribute, or the value of its form owner’s action attribute, if it has one, or else the empty string.

So they both still valid and works:

<form action="">
<form action="FULL_URL_STRING_OF_CURRENT_PAGE">

If you are sure your audience is using html5 browsers, you can even omit the action attribute:

<form>
6
  • Can you confirm the 'action' attribute can be left empty and validate ? Commented Aug 26, 2013 at 13:56
  • 7
    Doesnt validate. : Bad value for attribute action on element form: Must be non-empty.
    – Azd325
    Commented Feb 13, 2014 at 11:42
  • 5
    According to the HTML5 spec an empty string is not allowed as action: w3c.github.io/html/sec-forms.html#element-attrdef-form-action
    – Thomas
    Commented Sep 16, 2016 at 9:36
  • 1
    This is the only thing that works for me. <br/> link The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces........ if it has one, or else the empty string. Commented Jan 18, 2017 at 20:14
  • the concept of "doesn't validate" is absurd because the only person who can validate your XML is you. That's the point of XML - you say a bunch of rules that you want to hold true for your XML document, encode them in DTD and have the XML parser take of deceptively difficult parsing. This is one reason why XML is still important today - you can take any model you want, be it JSON, text, csv, write a simple transformer from that format to XML, write a DTD for that XML, and bam, you skip needing to write your own validator, XML already did that!
    – Dmytro
    Commented Jun 19, 2018 at 15:23
20

If you are submitting a form using php be sure to use:

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"

for security.

0

You can do it using the same page on the action attribute: action='<yourpage>'

Not the answer you're looking for? Browse other questions tagged or ask your own question.