Usually a HTML Form is created to enable people who visit a webpage to enter information or select from a set of predefined options.

HTML Form

HTML forms are used to pass data to a server, to be processed by a programming language (like PHP, ASP). HTML forms can also be used with script languages, like JavaScript.
The <form> ... </form> tag is used to create a HTML form.
<form>
  ... form elements
</form>

The <form> tag uses the following attributes:
A form can contain input elements like text fields, checkboxes, submit buttons; and select lists, textarea, fieldset, and more.
- All form elements are added inside <form> ... </form> tag.

Form - Input Elements

The input element is the most important form element. It is used to select user information, and can vary in many ways, depending on the type attribute.

Attributes of the <input /> tag:


Text field

Defines a one-line input field that a user can enter text into (the default width of a text field is 20 characters).
<input type="text" name="a_name" />
How it looks in a browser:  

Password field

type="password" creates a text input element, but the characters are masked (shown as asterisks or circles).
<input type="password" name="a_name" />
How it looks in a browser:  

Checkbox

Checkboxes let a user select one or more options of a limited number of choices.
<input type="checkbox" name="a_name" value="value" />
How it looks in a browser:  

Radio

Radio buttons let a user select only one of a limited number of choices, which have the same "name" attribute.
<input type="radio" name="a_name" value="value" />
How it looks in a browser:  

File

Allows users to submit external files to the server. It is used for file uploads.
- This field is accompanied by a "Browse" button, used to choose which document to be transferred to server.
<input type="file" name="a_name" />
How it looks in a browser:  

Simple button

type="button" defines a clickable button, that does not do anything. The button type is most often used to activate a JavaScript function when a user clicks on the button.
<input type="button" value="Click here" />
How it looks in a browser:  

Submit button

A submit button is used to send form data to a server. The data is sent to the page specified in the form's "action" attribute.
<input type="submit" value="Send" />
How it looks in a browser:  

Image for the Submit button

type="image" defines an image as a submit button. If a type="image" button is pressed, the form is submitted. The "src" and "alt" attribute are required.
<input type="image" src="image.jpg" alt="Submit" />
How it looks in a browser:  

Reset button

A reset button resets all form fields to their initial values.
<input type="reset" value="Start Over" />
How it looks in a browser:  

Hidden

A hidden field is not visible for the user. Hidden controls can be used to pass some data to the server that the user cannot see or alter.
<input type="hidden" name="a_name" value="hidden_value" />

Other form elements

Textarea field

The <textarea> tag defines a multi-line text input control which can hold an unlimited number of characters.
The size of a textarea can be specified by the cols and rows attributes, or through CSS, with height and width properties.
<textarea name="a_name" cols="20" rows="3"> Text content </textarea>
How it looks in a browser:  
    Attributes for textarea:

Select list

The <select> </select> tag is used to create a select list (drop-down list).
This element should contain one ore more <option>...</option> tags. This tag defines an option within the select item.
<select name="a_name">
 <option value="val1">Option 1</option>
 <option value="val2">Option 2</option>
 <option value="val3">Option 3</option>
 ...
</select>
- If the "value" attribute is not added in the <option> tag, its content will be the value that is sent to the server.
How it looks in a browser:  
    Attributes for <select>:
    Attributes for <option>:

<label> tag

The <label> tag defines a label for an input element.
Each label element is associated with one form control. The label element may contain the form control, or it may use the "for" attribute to identify the control by its "id" value.
  - Syntax:
<label>Text <input type="the_type" name="a_name" /></label>
Or with the "for" attribute:
<label for="input_id">Text</label> <input type="the_type" name="a_name" id="input_id" />
If the user clicks on the text within the label element, it toggles the control.
Example:
<form action="script.php" method="post">
 <label>Text: <input type="text" name="text1" /></label>
 <label for="idc">Check:</label> <input type="checkbox" name="a_name" id="idc" /><br />
 <input type="submit" value="Submit" />
</form>
This code will display:


<fieldset> and <legend> tags

The <fieldset> tag is used to logically group together elements in a form. It draws a box around the related form elements.
The <legend> tag defines a caption for the fieldset element.
Example:
<form action="script.php" method="post">
 <fieldset> <legend style="margin-left:90%;">Log in</legend>
   <label>Name: <input type="text" name="user" /></label><br />
   <label>Password: <input type="password" name="pass" size="16" /></label>
 </fieldset>
 <input type="submit" value="Submit" />
</form>
This code will display:
Log in

<optgroup> tag

The <optgroup> tag is used to group together related options in a select list. It uses two attributes: Example:
<form action="script.php" method="post">
 <p>What is your favorite fruit or vegetable?</p>
 <select name="fv" size="6">
 <optgroup label="Fruits">
  <option>Apple</option>
  <option>Banana</option>
 </optgroup>
 <optgroup label="Vegetables">
  <option>Cabbage</option>
  <option>Tomato</option>
 </optgroup>
 </select>
</form>
How the HTML code above looks in a browser:

What is your favorite fruit or vegetable?

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Forms and Input

Last accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (34340)
  2. Escape From Lonely Room (74)
  3. Image Map (1621)
  4. Magic Methods __get, __set, __call, __toString (2137)
  5. PHP Script Website Mini-Traffic (2833)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (45791)
  2. Read Excel file data in PHP - PhpExcelReader (34450)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (34340)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (30919)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (28165)