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

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.

PHP array_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute 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 array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP array_merge() Function

PHP array_merge() Function The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array...

1 minute read.

PHP strchr() Function

PHP strchr() Function The strchr() function in PHP searches for the first occurrence of the specified string inside another string. It is a binary-safe case -sensitive function and is the alias of strstr() function. Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] ) Parameter haystack(required)-...

1 minute read.

PHP substr_replace() Function

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP array_push() Function

PHP array_push() Function The array_push() function in PHP pushes one or more elements onto the end of the array. This function increases the length of the array by the number of variables pushed. Syntax array_push ( array &$array, mixed& value [, mixed $... ]...

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

PHP uksort() Function The uksort() function in PHP sorts an array by keys using a user-defined comparison function. Syntax uksort ( array &$array , callable $key_compare_func ) Parameter array(required)- This parameter represents the input array. key_compare_func(required)- This parameter represents a string that describe a callable comparison function....

1 minute read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

PHP array_diff_key () Function

PHP array_diff_key () Function The array_diff_key () Function in PHP compares the keys from array1 against the keys from arrays. It returns an array containing all the entries from array1 whose keys are absent from all other arrays. This function is like array_diff () except that...

1 minute 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 Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP is_iterable() Function

The is_iterable() function in PHP verifies that the contents of the specified variable is an iterable value. Syntax is_iterable ( mixed $var ) var(required)- This parameter represents the value to check. Return This function returns a Boolean value TRUE if the parameter var is iterable, else it returns...

1 minute read.

PHP array_reduce() Function

PHP array_reduce() Function The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array. Syntax array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) Parameter array(required)- This parameter represents the input array. callback (required)- This...

1 minute read.

PHP Date and Time

DATE: The PHP date() function formats a date or time. The timestamp is used to format a more readable date and time into a PHP date () function. Syntax: date(format,timestamp) Simple Date: Characters that...

1 minute read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.