One of the main features in PHP is the ability to take user input and generate subsequent pages based on the input. In this page, we will introduce the mechanism by which data is passed in PHP.
Let's consider the following two files:
query.php
<form action=result.php type=post> <input type=text name=employee> <input type=submit value=Submit> </form> |
<?php $employee_name = $_POST["employee"]; print $employee_name; ?> |
How is this done? The $_POST function captures the value associated with the key "employee", which was specified in the input name attribute in query.php. The variable $employee_name is then set to the value for this key.
$_GET is $_POST's closely related cousin. The usage is exactly the same, except you'll need to use $_GET if the form action type is GET. When the form action type is GET, the parameters you submitted will be visible in the URL as the query string. In a POST method, no query string is visible in the URL.
Usually when using $_POST or $_GET, you'll want to first check to see if PHP has indeed received values for the key of interest. If not, you may want to assign the receiving variable some type of default value. To do this, we leverage the isset() function:
$result_variable = isset($_POST["key_value"],0,$_POST["key_value"]); |
Common input methods include text input, radio button, checkbox, and drop down menu
Previous Page->PHP-Array || Next Page->PHP-cookies