Web forms are feature of HTML not PHP. But, the form data must be interpreted at server and can be stored or a response can be created and send back to the user as feedback. 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">
|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=”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.
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"]; ?><br>
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:
The length of URL is limited, so it is not possible to send more than 2000 characters using this method.
Note: Do not use “get” method to for sending passwords or e-mail addresses.
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.
Note: You can use “post” to send larger form data and also you can upload files using this protocol.
When you use “post” method, the information from your form is contained in $_POST variable. So, if you decide to use “get” than change your mind and use “post” instead you will need to refactor your server side code.
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.
This was the beginner tutorial for PHP. We will cover more topics after we have at least 10 students that pass exam with at least 80% score. Otherwise this tutorial will be continued in the technology class.
Thanks for reading, have a fun day.