简体   繁体   中英

html forms - why do I often see <input name=“next” />? Clarification on what the 'name' attribute does

I was always confused about what the 'name' attribute did in html forms. From the textbook I read (html and css, design and build webpages by John Duckett), this is what it said about the 'name' attribute.

When users enter information 
into a form, the server needs to 
know which form control each 
piece of data was entered into. 
(For example, in a login form, the 
server needs to know what has 
been entered as the username 
and what has been given as the 
password.) Therefore, each form 
control requires a name attribute. 
The value of this attribute 
identifies the form control and is 
sent along with the information 
they enter to the server.

From reading this, I always thought that, say in the database there is a field called "theUsersPasswordField" and a field called "theUsersUsernameField". I thought that, suppose there is a registration form, then the form would be like:

<form action="aURL" method="post">
    <p>Please enter what you want your Username to be:</p>
    <input type="submit" name="theUsersUsernameField" />
    <p>Please enter what you want your Password to be:</p>
    <input type="password" name="theUsersPasswordField" />
</form>

and then this way, when the information is sent to the database, it will know which information to put in the 'theUsersPasswordField" and which information to put in the "theUsersUesrnameField". Am I wrong?

What does name="next" mean? I see it often when I look at html forms, for example, here in this Django tutorial I am doing:

<form method="post" action=".">
    <p><label for="id_username">Username:</label></p>
    <p><label for="id_password">Password:</label></p>
    <input type="hidden" name="next" value="/" />
    <input type="submit" value="login" />
</form>

In the tutorial I am doing, it says that

The html form contains a submit button and a hidden
field called next. This hidden variable contains a URL that tells the view where to
redirect the user after they have successfully logged in

now, how is 'next' a url? When I run the code, the form does in fact successfully redirect to the main page, but how does it know to redirect to the main page? Why does name='next'?

And how does the server know which information to treat as the username and which information to treat as the password? I though that that is what the 'name' attribute is used for?

The name attribute in a control element like input assigns a name to the control. It has two basic effects: 1) a control needs a name in order to be “successful”, which means that a name=value pair from it will be included into the form data when the form is submitted; and 2) the attribute specifies what will be included as the first part of the name=value pair.

It is entirely up to the server-side form handler what (if anything) it will do with the name=value pairs in the form data. They might have a simple correspondence in some database, but that's just one possibility. And form handling need not be database-based at all.

The name attribute values have no predefined meaning in HTML. They are just strings selected for use in this context, and they may be descriptive or mnemonic, or they may not.

However, the choice of name attribute values may have side effects. Browsers may give the user a menu of previously entered data so that if you fill eg several forms (possibly in different sites) that have a control named email , you might be able to enter your email address just once and then accept whatever the browser suggests as input. This may be seen as a convenience or as a threat to data security. There is proposed set of “standard” name s for many purposes in HTML5 CR.

For completeness, it needs to be added that in browser practice and according to HTML5 CR description of name , two names have a special meaning: _charset_ and isindex .

The name next is in no way special, but in this context, it appears to specify the next page to move to. It is defined for a hidden field, so it takes effect independently of user input.

and then this way, when the information is sent to the database, it will know which information to put in the 'theUsersPasswordField" and which information to put in the "theUsersUesrnameField". Am I wrong?

You have to write a script (for example in php) that will put the right values from your form (they are in the $_POST array) into the databse.

in your example $_POST['theUsersUsernameField'] will hold the username

<form method="post" action=".">
<p><label for="id_username">Username:</label></p>
<p><label for="id_password">Password:</label></p>
<input type="hidden" name="next" value="/" />
<input type="submit" value="login" />
</form>

how is 'next' a url?

next is not the url.
the action="." is the url to wich the form redirects. / is the value that the script will evaluate to see what it has to do. (Normally you will have to change this into something else like 'check password')

In the $_POST[] array there will be a key $_POST['next'] and the value will be /

I am not familiar with Django but I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM