Form action="" and form action="?"

Hello,

I am reading “Build Your Own Database Driven Web Site, 4th Edition”.

In chapter 3 (page 109) the book introduces a rather strange way to use a form action.
<form action=“” method=“post”>
The book further explain that by sending an empty action, the form is submitted to the same PHP script.

In chapter 4 (page 132) the book introduces:
<form action=“?” method=“post”>
This time the book explains that by using action=“?” any data after the ? in the url will be deleted, basically deleting and GET information.

I am a bit confused as I cannot find any documentation around explaining these two ways of submitting a form. Can anyone point me on the right explanation/documentation in the php.net website?

Cheers
Luca

Now that I am thinking, could you put directly PHP code in the form action? Something like:
<form action=“<?php …php code… ?>” method=“post”>

This is not something specific to PHP, but general behavior of forms.
Indeed as you say, when you leave the action blank the form will be posted to the URL the user is currently on.

So for example if the user is on [noparse]http://blah/?a=1&b=2&c=3[/noparse], then that’s where the form will be submitted.

Now, if you fill in anything in the action that is not a full URI (i.e. does not start with a schema like [noparse]http://)[/noparse], it will be treated as a relative URL.
So for example, if I fill in “?” as action on a form on page [noparse]http://blah/?a=1&b=2&c=3[/noparse], it will post to location “?” relative to that URL, which is [noparse]http://blah/?[/noparse] (note that the ? at the end here is from “?” in the action, it is not the ? that was already there. The ? that was there originally was removed, and this one was put in its place).

A few more examples to hopefully make this a bit clear:

[noparse]http://blah/blah?a=1[/noparse], with form action “”, will post to [noparse]http://blah/blah?a=1[/noparse]

[noparse]http://blah/blah?a=1[/noparse], with form action “?”, will post to [noparse]http://blah/?[/noparse]

[noparse]http://blah/blah/?a=1[/noparse], with form action “”, will post to [noparse]http://blah/blah/?a=1[/noparse]

[noparse]http://blah/blah/?a=1[/noparse], with form action “?”, will post to [noparse]http://blah/blah/?[/noparse]

[noparse]http://blah/blah/?a=1[/noparse], with form action “/”, will post to [noparse]http://blah/[/noparse]

Does that make sense?

Hi ScallioXTX,

Yes, It starts to make sense. Does it means that:

http://blah/blah1/blah2?a=1, with form action “…”, will post to http://blah/blah1

In other words can I put any shell navigation command as an action?

Re “…” , almost, but not quite how you did it, it should end in a / , i.e., “…/”. I personally wouldn’t use this though as it may (and will) confuse people.

Re using php in action=“”, you sure can :slight_smile:

A pattern that is used a lot these days is:

  • Post to current URL (i.e., action=“”)
  • Check if the input is valid,
    [LIST]
  • if it is: redirect to another page (redirect to make sure you don’t get the “Resubmit form” error from the browser when you press the back key)
  • if it’s not: just show the page again and indicate the errors made on the form (by showing error messages and making the labels of erroneous fields red for example)
    [/LIST]

I use this all the time and it works perfectly well. The only reason I can think of where it would use an action other than “” is if I had to post some data to somebody else’s website in the browser for whatever reason (and indeed, that almost never happens).

ScallioXTX,

Great thanks for the crystal clear explanation.

Thanks Again
Luca