Submit HTML form on self page

HtmlFormsHttpUrl

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

Use ?:

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

It will send the user back to the same page.

Solution 3 - Html

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>

Solution 4 - Html

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

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

for security.

Solution 5 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOto ShavadzeView Question on Stackoverflow
Solution 1 - HtmlMilche PaternView Answer on Stackoverflow
Solution 2 - HtmlLe CodeurView Answer on Stackoverflow
Solution 3 - HtmlmuratgozelView Answer on Stackoverflow
Solution 4 - HtmlGeorgeButterView Answer on Stackoverflow
Solution 5 - HtmlDarkAjaxView Answer on Stackoverflow