PHP Form Handling or Processing
The form submitted by the client machine to the web server is actually received by a PHP script file, and it is known by action attribute of the form tag. The PHP Web server will store all the parameters received from the form in the $_GET or $_POST Global array.
The PHP script written to process the form actually access the $_GET or $_POST variable.
Example – 1 Form Handling Code with $_GET
<!DOCTYPE html> <!-- Login Page --> <html> <head> </head> <body> <form method="get" action="processlogin.php"> First name:<br> <input type="text" name="firstname"><br /> Last name:<br> <input type="text" name="lastname"><br /><br/> <input type="submit" value="Submit"> </form> </body> </body> </html>
<!-- Process(processlogin.php) page --> <?php print_r ($_GET); echo "<br />"; $firstname = $_GET['firstname']; $lastname = $_GET['lastname']; if (empty($firstname)) { echo "Name is empty"; } else { echo $firstname . " " . $lastname;; } ?>
In the above example, we create a form and that form have 2 attributes one id action attribute and another one is method attribute.
In action attribute, we define the target page where the form to be submitted and the method attribute is used to identify the submission method is used(GET or POST).
PHP Form Handling or Processing Example Code with $_POST
When you use the POST Method the data will stored in the $_POST array variable.
Example – 2 Form Handling Code with $_POST Super Global Array
<!DOCTYPE html> <!-- Login Page --> <html> <head> </head> <body> <form method="post" action="processlogin.php"> First name:<br> <input type="text" name="firstname"><br /> Last name:<br> <input type="text" name="lastname"><br /><br/> <input type="submit" value="Submit"> </form> </body> </body> </html>
<!-- Process(processlogin.php) page --> <?php print_r ($_POST); echo "<br />"; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; if (empty($firstname)) { echo "Name is empty"; } else { echo $firstname . " " . $lastname;; } ?>
If you set the method is $_POST than you have to used $_POST variable.
Form Handling or Processing with $_REQUEST Variable
When you use the REQUEST Method the data will be stored in the $_REQUEST array variable. Both GET and POST submitted variables are stored in the REQUEST global variable. If you are not sure about the methods of the variable you can use $_REQUEST Global variable
Example – 3 Form Handling with $_REQUEST Code
<!DOCTYPE html> <!-- Login Page --> <html> <head> </head> <body> <form method="post" action="processlogin.php"> First name:<br> <input type="text" name="firstname"><br /> Last name:<br> <input type="text" name="lastname"><br /><br/> <input type="submit" value="Submit"> </form> </body> </body> </html>
<!-- Process(processlogin.php) page --> <?php print_r ($_REQUEST); echo "<br />"; $firstname = $_REQUEST['firstname']; $lastname = $_REQUEST['lastname']; if (empty($firstname)) { echo "Name is empty"; } else { echo $firstname . " " . $lastname;; } ?>
If you set the method is $_POST than you have to used $_REQUEST variable.
I enjoy what you guys tend to be up too. This kind of clever work and reporting! Keep up the terrific works guys I’ve incorporated you guys to our blogroll.