PHP HTML Forms Using HTML Form Fields Hidden Radio Buttons Checkboxes

PHP is widely used today as server side scripting language. It is mostly used to develop interactive web sites and can easily be embedded in HTML. The interaction between user/browser and the server is carried out using the HTML forms. One of the most powerful features of PHP is the way it handles HTML forms. This tutorial explain using HTML form fields like Input, Hidden, Radio Buttons, Check boxes etc. The basic concept that is important to understand is that any form field values in a form will automatically be available to your PHP scripts. HTML forms can be submitted to the handling PHP script using either GET or POST method. See example below:

When we submit above form autoglobal $_POST['first_name'] and $_POST['last_name'] automatically available to the form handling PHP script. Similarly if GET method is used then $_GET['first_name'] and $_GET['last_name'] is available. Following example shows how we can access values in handling PHP script i.e. in action.php

There are various types of input elements are used in HTML form. Following is the list of such elements used to accept inputs from user/broswer that needs to be sent to the web server for further processing:

Text input, Hidden input and Password type input fields

This input form element can be used for a single lime text input from the user. For example user first name, user e-mail, phone numbers etc. The syntax in HTML form is as below

This will display an input box in the browser and the values of this element canbe accessed at the form handling php script by following.

Password type The defulat type of HTML input element is text. The special password type of input can be used as following

and can be accessed in PHP similar to above input element.

Hidden type The hidden field is used to send information from browser to the server without having to input it. Typically this is used to send some logical data that has nothing to do with user but used at the server in PHP program logic. For example state, action, or passing the result to the other module etc. Please note that you encode the value using htmlspecialchars() function.

and the value of this field can be accessed in PHP by following.

Textarea multiline text input HTML form element in PHP

This form element can be used for a multi-line text input from the user. For example product description. The syntax is as below.

This will display a multi-line input area in the browser and the size can be controlled using rows and cols tag. The text value input by user can be accessed in PHP by following

Select (Pull Down menu) and Select multiple options in PHP

This type of HTML form element is used to allow user to select from multiple choices. For example selecting a month or a day. The html syntax is as below.

The value of selected choice can be accessed in form handling PHP script as following

The variation to this allows user to select multiple choices from the given options. This can be done by following select multiple syntax

The size tag can be used to control how many rows should be visible. And note the use of '[]' following the name of the select box. This will denote that it is an array and the choices can be accessed in form handling PHP script by following.

Radio buttons HTML form element

Radio button in HTML form is an alternative method that allows user to select from given options. Unlike drop down menu this is used to create more visibilty in the input form.

The value of the selection can be accessed in PHP as following

Check Boxes HTML form element

This is an alternative way of allowing user to select multiple choices from the given options. Following example show the use of Check Boxes in an HTML form

Again here PHP array is used to access the selected choices from the user.


Anonymous said...

Your select box code does not work.

Anonymous said...

The reason why it doesn't work is because you misspelled the two variables namingly 'colours' and 'colors'

Anonymous said...

How do you do GET?

Hp said...

HTML form GET method.

[form action="action.php" method="GET"]
..html form code..

Anonymous said...

ok but how do you do rich text formats?

Anonymous said...

what about rich text formats?

Reeko said...

Checkbox problem!

foreach ($_POST['check'] as $key=> $value );

This line of code only post the last checkbox value, not the multiple choice I made. I must be doing something wrong?

Thanks for reply!


Outsourcing India said...

Thanks. It helped lot.


Anonymous said...

Thank you very much, I can program in several languages and just needed some basic syntax on how to access variables passed from my form. Just what I needed - thanks!

html password field said...

thanks for the good article.
html password field is the secure field is used for authentication.
You will find more tutorials regarding html password field in
html password field

Anonymous said...

I was wondering if you could tell me the best way to submit data from one web page to another using the submit method. I have the button created and everything but I'm at a loss for how to send my text area information to my other web page.
Thanks ahead of time!!!