PHP Forms

tutorial and example
tutorial and example

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:

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

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

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

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’;  orinclude”filename”;  or  include(“filename”);

Example

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

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

Output

today is:2018-01-03

//welcome.php

Output

Example2.D

// welcome2.php

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

Output