Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Adding a Contact Form Creating Input Fields

duncan borg
duncan borg
1,243 Points

Difference between name="name" and id="name"

i don't get why they are used

2 Answers

In an input field the name property is set in order for the value entered by a user to be accessed by php code running on the server and the id field is used as a way of identifying the element on the page. So in the example below the id field of the input type text is used by the label so its knows which input field it belongs too and the name field is used to access the value a user has entered. <?php if($_SERVER["REQUEST_METHOD"]=="POST"){ $username=$_POST["name"]; echo $username; } ?>

<html>
<body>
    <form action="" method="post">
    <label for="name">Username</label>
       <input type="text" name="name" id="name">
       <input type="submit" value="submit">
    </form>
</body>
</html>

hope this helps

From my humble understanding as a beginning,

In contact.php,

        <label for="name">Name</label>
        <input type="text" name="name" id="name">

       <input type="submit" value="Send">

id is used to identify HTML element input. name is used as a key when submit to when user click the Send button. (See the URL below after the user click the Send button)

http://localhost/shirts4mike/contact-process.php?name=Mike

      ?name=Mike is the query parameters
      name=Mike is the key-value pair
      name is the key and Mike is the value

Hope I am clear.

Konrad Pilch
Konrad Pilch
2,435 Points

Sharp , clear answer ^^ The other one is a bit to long and goes on beyond which is good but uh its too long : p