0

I am learning about HTML forms and in particular the action attribute has me a bit confused. What is the difference between the following values and when is it best to use each case?

action=""
action="?"
action="?page"
action="?page=main"
action="."
action="../"
action="/"
action="#"
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
GTS Joe
  • 3,612
  • 12
  • 52
  • 94
  • You can try them to see what they do, and there is no "best case" here. You just need to point to the script that will process your form. The first four all post back to the same page you're on. You need to narrow your question down a bit. – j08691 Apr 06 '14 at 19:51
  • That question is basically the same as if you were asking, _“what’s with the `href` attribute of a link, what’s best to use there?”_ – that would make the same little sense, because it of course depends on what you are trying to refer to with it. – CBroe Apr 06 '14 at 20:00

1 Answers1

0

From W3 Form Documentation:

This [action] attribute specifies a form processing agent

That is, the form, when submitted, sends the values to wherever the action is set to. For the most part your noted actions all submit the form back to the same page the form is displayed on. You could also leave the action out all together or use <?php echo $_SERVER['PHP_SELF']; ?> (if you're on a php page) to obtain the same effect.

Now, "?page=main" will technically go to the same page as well, but with the page GET value set to "main" (might be used for processing the output somehow for example). Use it if you need the page value, otherwise do one of the blank ones.

Community
  • 1
  • 1
DACrosby
  • 11,116
  • 3
  • 39
  • 51