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 pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

1 minute read.

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP trim() Function

PHP trim() Function The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string. The trim() function strips the following characters: " " - an ordinary space."\t"- a tab."\n"- a new...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP Functions

PHP functions are used to reuse piece of code many times.  There are various built-in functions in PHP. A function will be executed by a call to the function. Following are...

3 minutes read.

PHP Tutorial

PHP Tutorial PHP Introduction PHP is a server-side scripting language which is used for web development. It stands for Hypertext Preprocessor. It is faster than another programming language. It supports multiple databases like MySQL, SQL, PostgreSQL and...

3 minutes 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 addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute 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.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.