Pranav Jituri
Pranav Jituri

Reputation: 823

Concept Of Name And ID in PHP & HTML

I am doing programming in PHP and HTML these days. But the problem I am facing is that many times the data is accepted by the PHP engine using NAME attribute used in the HTML syntax of Forms but many times (like when there was a multi-radio button program) ID and NAME together gave me the required output.

Can anyone here give me a nice explanation of the concept and the difference between these two? I already tried googling but I could not understand.

Help will be appreciated :)

Upvotes: 0

Views: 773

Answers (1)

Quentin
Quentin

Reputation: 944538

The name attribute is used on form controls (like <input>) to associate a known label with a (possibly) variable value that will appear in the submitted data. It does not have to be unique, but PHP will only handle multiple fields which share a name properly if that name ends in [].

<input name="foo" value="bar"> will be available through $_POST['foo'] or $_GET['foo'] when the form is submitted.

The id attribute can be used on any HTML element so it can be referenced with client side technologies (such as <label for>, JavaScript and fragment identifiers in URIs). It does have to be unique.

NB: It is possible to use a name attribute to reference an element with client side technologies, but it is almost always a better idea to use id (or class for groups of elements) rather then adding a name attribute.

Upvotes: 2

Related Questions