146

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236

5 Answers5

174

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milche Patern
  • 19,632
  • 6
  • 35
  • 52
31

Use ?:

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

It will send the user back to the same page.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Le Codeur
  • 367
  • 3
  • 2
26

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>
Neuron
  • 5,141
  • 5
  • 38
  • 59
muratgozel
  • 2,333
  • 27
  • 31
  • Can you confirm the 'action' attribute can be left empty and validate ? – Milche Patern Aug 26 '13 at 13:56
  • 6
    Doesnt validate. : Bad value for attribute action on element form: Must be non-empty. – Azd325 Feb 13 '14 at 11:42
  • 4
    According to the HTML5 spec an empty string is not allowed as action: http://w3c.github.io/html/sec-forms.html#element-attrdef-form-action – Thomas Sep 16 '16 at 09:36
  • 1
    This is the only thing that works for me.
    [link](http://w3c.github.io/html/sec-forms.html#element-attrdef-form-action) 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.
    – Joel Bondurant Jan 18 '17 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 Jun 19 '18 at 15:23
  • The point of XML is to be extensible, not rigid. DTD is for YOU to verify what matters for YOU in your model. The browser couldn't care less whether or not you are breaking w3c's model that is for THEM not you. HTML model is useful for checking whether your model conforms people's expectations of what an HTML file looks like, but most of the time those expectations are very limiting and irrelevant to your model. Don't make people hate XML technologies as something limitated and rigid, they aren't! – Dmytro Jun 19 '18 at 15:25
18

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

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

for security.

GeorgeButter
  • 2,521
  • 1
  • 29
  • 47
0

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

DarkAjax
  • 15,955
  • 11
  • 53
  • 65