logo
Search:

Login:


Forgot Details? Sign-up

forum >> Programming questions >> HTML / XHTML / HTML5 / CSS

Question concerning the NAME attribute

Posted Apr 25 2012 at 6:55 PM by
Pete Woodhead (petewdhd)
Quote:
print "select name = \"book\" \n"; (sorry, I had to remove the less than and greater than symbols as they were causing the code to disappear.)

Harris, Andy (2010-10-28). HTML, XHTML and CSS All-In-One For Dummies (p. 567). For Dummies. Kindle Edition.

Hi Andy,

First of all arrays really are cool!

After I did the lesson "arraysHTML.php", I was reviewing the source code. I noticed that it showed the name attribute which you named "book". It got me to thinking so I looked up what you said about using name attributes when passing data server side on page 524:
Quote:
5. Give each element a name attribute. If you want a form element to be passed to the server, you must give it a name attribute. Note: This is a different attribute than id, which is used in client-side processing.

Harris, Andy (2010-10-28). HTML, XHTML and CSS All-In-One For Dummies (p. 524). For Dummies. Kindle Edition.

Since it did not seem that any data was being passed from the list to the server I played around with the code and fond that I could remove the name attribute completely from the select tag without disrupting the code and the XHTML validated as well.

So question is: Did you use the name attribute with the select element in this case so that the data could be called from the list with the aid of additional programming? My guess is that was the reason.
AuthorMessage
Pete Woodhead
Posted: Apr 26 2012 kl. 12:13 PM

My question is answered further along in book V, Chapter 4 in the section, "Introducing Multidimensional Arrays". The distance calculation examples made it clear to me. Where the select element name attribute is used to pass the array keys and values to the PHP program. I'm leaving the question on the forum is case it helps another who struggles as much as I do. XD
Andy
Posted: May 15 2012 kl. 10:18 PM

Sorry, Pete. I've been out of town for a few days.

You've figured it out, but let me clarify for anyone else. The ID and NAME attributes were originally considered essentially the same, but they now have subtly different roles. Both allow you to associate some sort of 'tag' to an element.

The ID is used on the client. JavaScript and CSS refer to an element by its id. The ID must be unique; that is, no two elements on a page can have the same ID.

In server-side programming, the elements themselves don't matter. You don't usually even know if information came from a text box or a radio button. Instead, all the contents of a form are broken into name - value pairs. The name comes from the NAME attribute of the original form element, and the VALUE is the value of that element. As far as the PHP program is concerned there's always exactly one name for one value.

However, this is where it gets kind of weird. In an HTML form, you often have several elements with the same name and different values. The classic example is the radio button. All the radio button elements might have a different id, but they'll all have the same name. The ID is processed on the client. Only one of the name - value pairs is sent to the server.

If in doubt: Use ID for client-side processing, NAME for server-side.
Pete Woodhead
Posted: May 16 2012 kl. 10:04 AM

Thank god! I thought I'd been abandoned to my own devices. :)