CodeBlanks Logo Icon CodeBlanks

Creating HTML forms

PHP course · Beginner · Lesson 100/116

Learn to create forms with method and action attributes.

What is a form?

An HTML form is the structure where a user enters data and sends it to the server. The <form> tag takes two core attributes: method decides how data travels, action decides where it goes. method="post" carries data hidden in the request body; method="get" appends it to the URL.

Inside the form you place <input> fields and a submit button. Every field needs a name attribute; PHP reads the value by that name. Example: <form method="post" action="/register.php"><input type="text" name="username"><input type="submit" value="Send"></form>.

Input fields and name

A form holds various input fields. type="text" makes a single-line text box, type="email" an email box, type="password" a password box (characters show as dots), type="submit" a submit button. Every field needs a name; PHP reaches it inside $_POST by that name.

Example: <input type="email" name="email">. type controls the field behaviour, name sets the key PHP will use. Even the submit button can carry a name: <input type="submit" name="send" value="Send">.

Exercise Print the form HTML with echo

Print an HTML form. Use method="post", action="/register.php". Inside it place <input type="text" name="username"> and <input type="submit" value="Send">. Close the form with </form>. Write everything in a single echo.

Show example solution
Write the form in a single echo. Use method="post" and action="/register.php", add text and submit inputs inside, close with </form>.
Example solution:
<?php
// Print the form HTML with echo
echo '<form method="post" action="/register.php"><input type="text" name="username"><input type="submit" value="Send"></form>';

Test yourself

  1. Which method should you use to send a password field to the server?

    • GET
    • POST
    • REQUEST
    • FORM
    Show answer

    POST — Correct. POST carries data in the request body, not in the URL; it fits sensitive values like passwords.

  2. Complete the method value in the form tag

    echo '<form method="____" action="/register.php"></form>';
    Show answer
    echo '<form method="post" action="/register.php"></form>';
  3. Match each form attribute to its purpose

    method
    How data travels
    action
    Where the form goes
    name
    PHP array key
    type
    Field behaviour

All lessons in Beginner