Let’s review some useful "tags" that you should have learned in "HTML+CSS" class.
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"|
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.
<html> <body> <form action="welcome.php" method="get"> <input type="text" name="name" value="Enter your name"><br> <input type="submit" value="Submit"><br> </form> </body> </html>
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"]; ?></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.
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.
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?>"> <br> <input type="submit" value="Submit"> </form>
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: