PHP Forms and User Input
Previous Page | Home Page | Next Page |
---|
PHP Form Handling
Elements in an HTML page willautomatically be available to the PHP scripts.
Example
The example below contains an HTML formwith two input fields and a submit button:
<html> <body>
Name: <inputtype="text" name="fname" /> Age: <inputtype="text" name="age" /> <input type="submit"/> </form>
</html>
|
When a user fills out the form aboveand clicks on the submit button, the form data is sent to a PHP file,called "welcome.php":
"welcome.php" looks like this:
<html> <body>
Age:<?php echo$_POST["age"]; ?>
</html>
|
Output:
Welcome Ajay!
Age: 28
The $_GET Variable
The predefined $_GET variable is usedto collect values in a form with method="get"
Example
<formaction="welcome.php" method="get"> Name: <inputtype="text" name="fname" /> Age: <inputtype="text" name="age" /> <input type="submit"/> </form> The "welcome.php" file cannow use the $_GET variable to collect form data. (The names of theform fields will automatically be the keys in the $_GET array) Welcome <?php echo$_GET["fname"]; ?>.<br /> Age: <?php echo$_GET["age"]; ?>
|
The $_POST Variable
The predefined $_POST variable is usedto collect values from a form sent with method="post".
Example
<formaction="welcome.php" method="post"> Name: <inputtype="text" name="fname" /> Age: <inputtype="text" name="age" /> <input type="submit"/> </form>
|
The "welcome.php" file can now use the $_POST variable to collect form data :
Welcome <?php echo$_POST["fname"]; ?>!<br /> Age <?php echo$_POST["age"]; ?> |
Previous Page | Home Page | Next Page |
---|