php and web forms
December 31, 2007 // No Comments
web forms are one of the most common elements of a webpage and the most general method to input data from the user. PHP allows you to easily handle the data input by the user. All the elements of the form are automatically available to the PHP scripts. Consider the HTML page
<form action="dataHandler.php" method="post"> <p>first name: <input type="text" name="fname" /></p> <p>last name: <input type="text" name="lname" /></p> <p><input type="submit" /></p> </form>
the above code plainly defines a simple html form with two text input elements. When the user fills the data in the text field and click on the submit button, the dataHandler.php is called which is of the following type
Hello <?php echo htmlspecialchars($_POST[’fname’]);?> Your last name is <?php echo htmlspecialchars($_POST[’lname’]);?>
in the HTML form, the method specified was POST, therefore the data was stored in the variable $_POST. Another option is to use the method GET. When GET method is used with a form, the data from the form is stored in the variable $_GET.
Another variable available is $_REQUEST which contains the merged information from the GET, POST and COOKIE data.