PHP Forms




PHP Tutorial > PHP Commands > Forms

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>

result.php

<?php
  $employee_name = $_POST["employee"];
  print $employee_name;
?>

After the user types in a value in the text box and click on the Submit button in query.php, the result.php page will display the value that the user has just typed in.

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"]);

In the above example, if "key_value" is not set, $result_variable is set to 0.

Common input methods include text input, radio button, checkbox, and drop down menu.

Next: PHP Cookies



Copyright © 2014   1keydata.com  All Rights Reserved.  Privacy Policy