Jump to content

Simple form question


Bman900

Recommended Posts

Hi there, I am still learning here and I just have a quick question about this code here:

 

<html>
<input type="text" id="firstname" name="firstname" />
</html>

 

What is the point of the "id" tag? I have been watching some tutorials and they seem to just skip that and only set the type and use the name attribute. Thanks in advance.

Link to comment
Share on other sites

The ID and NAME do different things.

 

ID is for two main things. The first is for styling the tag using CSS. Ids are only allowed to be used once per page, so if you need to target a specific element for styling, you can give it an ID, and style that element and only that element (there are also classes which can be used on multiple elements in a page, and allow for styling all of them at once). The other use for IDs is in Javascript. Basically it does the same thing as CSS - it allows a javascript function to target that element and only that element, and do something to it.

 

The NAME attribute is completely different. When you are creating form element and you submit them, the script you submit them to has to know what values you submitted. What happens is that the submitted value is tied to the NAME attribute. So if you enter 'haku' into the input you put above, the value of 'haku' as accessible to the script using the NAME 'firstname':

 

$first_name = $_POST['firstname'];

 

The code above (which is PHP code) creates a variable, which is like a container, called $first_name, then it puts a value into that variable (container). The value it puts in is the value that was in the <input> tag with a NAME of 'firstname'.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.