43

Here is the form that is confusing me

<h1>
    Login
</h1>
<form action="" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
        <tr>
            <td>
                Username:
            </td>
            <td>
                <input type="text" name="user" maxlength="30">
            </td>
        </tr>
        <tr>
            <td>
                Password:
            </td>
            <td>
                <input type="password" name="pass" maxlength="30">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <input type="checkbox" name="remember">
                <font size="2">
                    Remember me next time
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <input type="submit" name="sublogin" value="Login">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <a href="register.php">Join</a>
            </td>
        </tr>
    </table>
</form>

I got the code from this tutorial and it works fine but i cant seem to understand where a/the form submit too if no action is present

Shaz
  • 15,192
  • 3
  • 40
  • 58
Matt Elhotiby
  • 41,184
  • 82
  • 214
  • 317

3 Answers3

59

If action is set to "" or if the action attribute is missing, the form submits to itself. That is, if your script is index.php, your form submits to index.php.

Flimm
  • 115,689
  • 38
  • 227
  • 240
Jimmy Sawczuk
  • 13,208
  • 7
  • 46
  • 60
14

If the form's action attribute is either set to "" OR not specified, it will still default to action="self", thus the form will send to the address of the document containing the form.

So,

<form method="post">

<!-- IS THE SAME AS... -->

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

<!-- IS THE SAME AS... -->

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

(Try it)

For reference, see the HTML standard 4.10.18.3 #8:

http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#form-submission-algorithm

Old McStopher
  • 6,195
  • 9
  • 59
  • 86
  • 3
    `action="self"` submits to **/self** which is wrong. Tested this to be sure and it does **not** do the same thing as empty attribute. Searched specs and see no mention of "self" in this context – Eaten by a Grue Jun 23 '17 at 23:11
  • I believe he was just using "self" as an explanation, not an example. – RationalRabbit Sep 27 '17 at 22:01
6

default action for form submission is METHOD="GET" and ACTION="SELF"
you should use a form name

if the action is empty then it posts to itself.

Sourav
  • 16,318
  • 34
  • 98
  • 153
  • 3
    does that mean if you go
    instead of
    (leaving out action entirely) it still posts to itself? or does this just work in some browsers.
    – robert king Mar 02 '12 at 02:23
  • 1
    @robertking, see my answer for this, including examples and a reference to the HTML standard. – Old McStopher Jun 29 '14 at 08:23
  • 2
    another person posting about how `ACTION="SELF"` submits to self.. where are you guys getting this from? That's not how it works. I know this is 6 years old but I don't think it worked like that 6 years ago either. – Eaten by a Grue Jun 23 '17 at 23:19