Search code examples
htmlformshttp-redirect

HTML forms - difference between 'action' and the 'next' variable?


According to the HTML book which I am reading (and according to here: http://www.w3schools.com/tags/att_form_action.asp) it says that in the case of forms:

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

'action' specifies where to send the form data when a form is submitted. The syntax for it could be

<form action="URL">

Now, in the book I am reading, it also talks about a hidden 'next' variable, like so:

<form action="/login/" method="post">
<input type='hidden' name='next' value='/' />
<input type='submit' value='login' />

The book I am reading states that

The form contains a submit button as well as a hidden field called 'next'. This hidden variable contains a URL that tells where to redirect the user after they have logged in.

From my understanding, doesn't 'action' either way tell specify where to redirect to after the form has been submitted? So isn't having the hidden 'next' variable not necessary because 'action' already tells where to redirect to? Which takes priority if action and next are different URLs? Does it redirect to the URL in action or the URL in next?


Solution

  • First up the action attribute has nothing to do with redirection.

    When you click a submit button in a form the browser sends an HTTP request to the server, specificaly to the resource mentioned in the action attribute, using either get or post. What happens next is entirely dependant on what you are using server side.

    As far as I am aware the presense of a Next property in the request has no special meaning.

    Normally when a form is submitted one of two things will happen

    • The server does some process then return an HTTP Response. This will show the action url in the address bar of the browser
    • The server does some processing then redirects the user to a new page. This is usualy programmed by you the programmer and does not happen automagicaly. In php you would use http_redirect(someURL). The next hidden field could be used to hold this URL but it won't do anything with it by itself.

    On a technical note, http redirects, be they with asp, php, etc cause, an additional round trip to the browser. So in the case of the redirect above, an HTTP Response is send to the broswser with a header indicating to redirect and where to direct to. The browser will then send a new request to the new location. This is why the new address appears in the browser address bar.