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.
Note: The Put, Patch and Delete methods are used in MVC( Model View Controller) with Framework in PHP. PHP $_REQUEST: PHP $_REQUEST is a super global variableused to collect the  data through http request after submitting  the html form. Example:
<?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 In PHP, there are various request methods.
  • Get method
  • Post method
  • Put method
  • Delete method etc.
Note: We will discuss later put and delete method. PHP Get Form PHP get request is the default form request. It is used to pass the data through the URL onbrowser by encoding the information with “?”sign. Example1
<?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 Example2 // welcome.php
<?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 Following are the differences between Post and Get method.
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
  PHP include& Require File PHP include & required file is used to include the same piece of content on the multiple pages. It helps to manage a siteeasily. Following ways to include files in PHP.
  1. Include and required File
  2. Include once and require once statement
  • PHP include andrequire statements.
Include Statement The include statement is used to include a file into the PHP code. However, when we include php file, which is not found, then the script will be executed continue. Syntax:
&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 Example2.D // welcome2.php
<html>
<body>
<h1>Example of require_once()</h1>
<?php
require_once('x.php');
require_once('x.php');
?>>
</body>
</html
PHP include_once() If above file “x.php” statement is included twice with required_once() statement then it ignores all the similar inclusions after the first one. Example // welcome.php
<html>
<body>
<h1>Example of include_once()</h1>
<?php
include_once('x.php');
include_once('x.php');
?>
</body>
</html>
Output