s_eye — 2011-11-08T13:17:04-05:00 — #1
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?
s_eye — 2011-11-08T15:20:01-05:00 — #2
Now that I am thinking, could you put directly PHP code in the form action? Something like:
<form action="<?php ...php code... ?>" method="post">
scallioxtx — 2011-11-08T14:44:41-05:00 — #3
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 http://blah/?a=1&b=2&c=3, 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 http://), it will be treated as a relative URL.
So for example, if I fill in "?" as action on a form on page http://blah/?a=1&b=2&c=3, it will post to location "?" relative to that URL, which is http://blah/? (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:
http://blah/blah?a=1, with form action "", will post to http://blah/blah?a=1
http://blah/blah?a=1, with form action "?", will post to http://blah/?
http://blah/blah/?a=1, with form action "", will post to http://blah/blah/?a=1
http://blah/blah/?a=1, with form action "?", will post to http://blah/blah/?
http://blah/blah/?a=1, with form action "/", will post to http://blah/
Does that make sense?
s_eye — 2011-11-08T15:08:22-05:00 — #4
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?
scallioxtx — 2011-11-08T17:13:27-05:00 — #5
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
A pattern that is used a lot these days is:
- Post to current URL (i.e., action="")
- Check if the input is valid,
- 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)
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).
s_eye — 2011-11-10T12:44:59-05:00 — #6
Great thanks for the crystal clear explanation.