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

Related Topics

PHP range() Function

PHP range() Function The range () function in PHP is used to create an array containing a range of elements. Syntax range ( mixed $start , mixed $end [, number $step = 1 ] ) Parameter start(required)- This parameter represents the first value of the sequence. end(required)- This parameter represents the...

1 minute read.

PHP array_walk_recursive() Function

PHP array_walk_recursive() Function The array_walk_recursive () function in PHP is used to apply a user-defined callback function recursively to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk_recursive ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents the...

1 minute read.

PHP Validation

PHP validation is used to check whether the field is filled or not in the proper way by the user. There are two types of validation in PHP. Client Side Validation:...

5 minutes read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP asort() function

PHP asort() function The asort() function in PHP is used to sort an array while maintaining the index association. Syntax asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  array(required)- This parameter represents the input array to sort. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

PHP money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

1 minute read.

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP print_r() Function

PHP print_r() Function The print_r() function in PHP displays the information stored in a variable. Syntax print_r (mixed $expression [, bool $return] ) Parameter expression(required)- This parameter represents the expression to be printed. return(optional)- This parameter is used to store the output of...

2 minutes read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.

PHP ksort() Function

PHP ksort() Function The ksort() function in PHP sorts an array by key while maintaining the keys. Syntax ksort(array & $array[,int $sort_flags = SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This parameter is...

2 minutes read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.