PHP Tutorial Getting user input in PHP using HTML form

PHP Tutorial: Getting user input in PHP using HTML form

There are basically three ways in PHP to get user input. They are
  • Using HTML Form
  • Using URL values links
  • Using COOKIES
In this tutorial I shall talk how to use HTML Form to get user input and retrieve that values from web server. Form is simple HTML form start with
tag and end with

. The form is posted to the server using POST method and each data user has entered is stored on a PHP super global variable $_POST.  $_POST is an associative array that stores variables in key => value format. Let us take an example of simple HTML form with two fields: username and password.

        Simple Form



The name of the field username is ‘username’ and field password is ‘password’. The name actually stores as index in $_POST array and we can retrieve the value using this name as index of the array. For example to retrieve the value of username, we can print $_POST'[‘username’] and similar is the case for password. So lets make another file retrieve.php to print the values of username and password.

    echo "
    echo "

    echo "Username : {$_POST['username']}" ."
    echo "Password : {$_POST['password']}" ."

Now this retrieves the value of username and password. print_r is used in php to print the whole array. When above codes are run in the browser and input is given as ‘Bibek’ for username and ‘Subedi’ for password, then the output of retrieve.php will be like this

PHP Form Output