PHP Forms
PHP form is used to take input from users, by using superglobals$_REQUEST, $_GET and $_POSTmethod. There are various request methods in PHP
- Get method
- Post method
- Put method
- Patch method
- Delete method etc.
<?php if(isset($_GET["name"])){ $name=$_REQUEST['name']; echo $name; } ? <html> <body> <form action = "welcome.php" method = "GET" > <input type = "text" name = "name" / placeholder="Enter Your Name"><br> <input type = "submit" /> </form> </body> </html>Output


- Get method
- Post method
- Put method
- Delete method etc.
<?php if( $_GET["name"] || $_GET["age"] || $_GET["course"] ) { echo "Your name is ". $_GET['name']. "<br />"; echo "You are". $_GET['age']. " years old"."<br/>"; echo "You are in class ".$_GET['course']; exit(); } ?> <html> <body> <form action = "<?php $_PHP_SELF ?>" method = "GET"> <input type = "text" name = "name" / placeholder="Enter Your Name"><br <input type = "text" name = "age" placeholder="Enter Your Age"/><br> <input type = "text" name = "course" placeholder="Enter Your course"/></br> <input type = "submit" /> </form> </body> </html>Output


<?php if(isset($_GET["name"],$_GET["age"],$_GET["course"])) { echo "Your name is ". $_GET['name']. "<br />"; echo "You are". $_GET['age']. " years old"."<br/>"; echo "You are in class ".$_GET['course']; exit(); } ?>// form.html
<html> <body> <form action = "welcome.php" method = "GET" > <input type = "text" name = "name" / placeholder="Enter Your Name"><br> <input type = "text" name = "age" placeholder="Enter Your Age"/><br> <input type = "text" name = "course" placeholder="Enter Your course"/></br> <input type = "submit" /> </form> </body> </html>In the given above example, we have used isset() method to check whether a variable is set or not. Note: We can write php code with html without any problem. PHP POST Method PHP request methodis used to submit the form and get the data which is not visible on URL browser. It is secured and can be send the large amount of data. Example
<?php if(isset($_POST["name"],$_POST["age"],$_POST["course"])) { echo "Your name is ".$_POST['name']. "<br />" echo "You are". $_POST['age']. " years old"."<br/>"; echo "You are in class ".$_POST['course']; exit(); } ?> <html> <body> <form action = "welcome.php" method = "POST" > <input type = "text" name = "name" / placeholder="Enter Your Name"><br> <input type = "text" name = "age" placeholder="Enter Your Age"/><br> <input type = "text" name = "course" placeholder="Enter Your course"/></br> <input type = "submit" /> </form> </body> </html>Output


POST Method | GET Method |
The value does not visible in the URL | The value is visible in the URL. |
It is secure. | It is not secure. |
No limitation of length while submitting form. | Limitation of length while submitting form. |
High performance. | Lower performance. |
It supports different data types such as string, numeric, binary etc. | It supports only string data types because the values are displayed in the URL. |
Result can’t be bookmarks. | Result can be bookmarked due to the visibility of the values in the URL |
- Include and required File
- Include once and require once statement
- PHP include andrequire statements.
&include 'filename' or include"filename" or include("filename");Example
<html> <body> <h1>Example of include</h1> <?php include 'noFileExists.php'; echo "I am PHP Developer since $year"; ?> </body> </html>Output It will generate an errorbecause it could not find the 'noFileExists.php' but next line of the statement will be executed in include. Require Statement In require statement, if we execute the same code in require statement then echo message will not executed because the script execution dies after require statement and returned a fatal error. Syntax:
&require 'filename'; orinclude”filename”; or require("filename");Example
<html> <body> <h1>Example of require</h1> <?php require 'noFileExists.php'; echo "I am PHP Developer since $year"; ?> </body> </html>Note: We should use require when the file is required by the application. We should use include when the file is not required and application is being continued when the file is not found. 2). Include_once and require_once statemen PHP require_once(): If “x.php” statement is calling “y.php” statement with require_once() and does not find “y.php” statement, “x.php” statement will stop execution and causing a fatal error. Syntax:
&include_once('name of the called file with path');Example1. // x.php
<html> <body> <?php echo "today is:".date("Y-m-d"); ?> </body> </html>Output today is:2018-01-03 //welcome.php
<html> <body> <h1>Example of require_once()</h1> <?php require_once('y.php'); require_once('x.php'); ?> </body> </html>Output

<html> <body> <h1>Example of require_once()</h1> <?php require_once('x.php'); require_once('x.php'); ?>> </body> </html

<html> <body> <h1>Example of include_once()</h1> <?php include_once('x.php'); include_once('x.php'); ?> </body> </html>Output
