Home > Blockchain >  Does the name and id need to be the same in a HTML form input
Does the name and id need to be the same in a HTML form input

Time:07-27

Does the name and id of an HTML form input need to be the same? Because everywhere I've seen code it is the same. Is it needed or is it just standard

CodePudding user response:

As shown in this example:

<label for="username">Enter your username:</label>
<input id="username">

id can be used to connect inputs with their labels. Also you can use id to find the element in JS (DOM manipulation) and add css styles to that input. Although you can do both with name, it's not common practice. In languages like PHP or/and in forms sending data to some url (without reading the form data without getting the values of input items in JS and then sending it through ajax/fetch) name is used to label the data being sent to destination.

for example in PHP we read input values like this:

$var = $_GET['some_input_name'];
$var_posted = $_POST['some_other_input_name_but_with_post']

in this does not work with id.

if you send a form data to some url with GET method, you can see your input names added to the action (destination) url. for example:

<form action="http://example.com/" method="get">
    <input name="username" type="text">
</form>

will send and attach data to: http://example.com/?username=text_entered_in_the_input

CodePudding user response:

No, it does not have to be the same.

  •  Tags:  
  • html
  • Related