Using PHP variables taken from an HTML form
Posted on May 22, 2011 | Last updated: January 13, 2012 at 02:05 GMT
Processing HTML forms is one of the most common applications involved in real-world PHP code. Two files are going to be used in this example. The first one only contains HTML and the second one includes PHP.
Notice that the form's action is set to phpcode.php, which is the PHP file that will have access to the variables sent from the HTML form by using the POST method.
The value of the action attribute is the URL that will be loaded when the user clicks the Submit items button. The data the user has typed in the text boxes will be sent to this URL via the method specified in the method attribute, either get (appended to the end of the URL) or post (sent as a separate message).
htmlform.php file
phpcode.php file
Using the GET method in the example above would require to use $_GET['value1'] and $_GET['value2'] instead of $_POST['value1'] and $_POST['value2']. Alternatively, $_REQUEST['value1'] and $_REQUEST['value2'] can be used to access the data regardless of the method that is being used (GET or POST).