Sage-Code Laboratory

PHP Web Forms

Web forms are feature of HTML not of PHP. But ... the form data must be interpreted at server side. Data can be stored or response can be created and send back to the user as feedback in a web page. PHP is very good at these things.


Let’s review some useful "tags" that you should have learned in "HTML+CSS" class.

Form tag: <form>

Create a form and specify how the form is handled using attributes,

<form action="/action_page.php" method="get">
Input Type Description
action Defines server side PHP script to interpred the form data
method Define method type: "get" or "post"

Input tag: <input>

Create a form control, depending on attributes:

Input Type Description
<input type="text"> Defines a one-line text input field
<input type="radio"> Defines a radio button (for selecting one of many choices)
<input type="submit"> Defines a submit button (for submitting the form)


Next example is a web form that uses method "get" to send information to welcome.php script.

    <form action="welcome.php" method="get">
       <input type="text" name="name" value="Enter your name"><br>
       <input type="submit" value="Submit"><br>

Note: Form action is called form handler and is a script that do not contains the form. It contains a program that interpret the data send by method "get" or "post" to server when you press submit button.

Action handler

The form handler is a PHP script specified by "action" attribute in a web form. Handler can use several predefined variables scaled "superglobal" variables. These variables are associative arrays, containing information about form data:

When user press the submit button, the browser will create a post or get message and send this message to form action handler. The server can read the date from "superglobals" and prepare a response.


A simple handler for previous example: welcome.php


Hello: <?php echo $_GET["name"]; ?></p> </body> </html>

Note: Since the form method = "get" the variable $_GET will contain a value for field "name". We can find this value using $_GET["name"] since $_GET is in fact an associative array.

Get vs Post

You can use two methods to send information from your form to the server: "get" and "post". Decision when to use one or the other it belongs to you. There are several capabilities of each that you can consider before make your decision:

The "get" method: name and value pairs are encapsulated in URL request. Input values are stored in format "?field_name=field_value" separated by "&". In our case the URL could look like this:


Using "post" method: names and value pairs created by the form are embedded within the body of the HTTP request. This is more secure since the information is not visible, but it still require https: protocol to make a secure communication with the server.


Initial Value

The web form can be created with help from PHP. That’s the secret of dynamic web-forms. Instead of making plain HTML forms, you can use a *.php script to generate the form. 

One intresting thing you can do is to put the initial value in the form fields using "value" attribute. By using a php script you can set the initial values using "<?=$variable>" so the form is not going empty. 


This form contains PHP tags to set the initial values in controls.

<form name="Test" action="" method="post">
    <input type="hidden" name="ID" value="<?=$uid?>">
    <input name="name" value="<?=$name?>">
    <input type="submit" value="Submit">

Note: The variables you are using in the form for "values" attributes need to be loaded from a database or from $_POST global variable by name. The name of the input field becomes the key for the value in the associative array.

The End: Thanks for reading, I wish you a productive rest of the day!

Read next: Reference Manual: