Unit 6: Making Web Forms

4th semester

PHP Form Handling
 The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
Example
 The example below contains an HTML form with two input fields and a submit button:


When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called “welcome.php”:

“welcome.php” looks like this:

Welcome John

Your email address is [email protected]

Form Validation

 User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load.
You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
The built-in $_GET function is used to collect values in a form with method=”get”.

i.The $_GET Function

 The built-in $_GET function is used to collect values from a form sent with method=”get”. Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser’s address bar) and has limits on the amount of information to send (max. 100 characters).
Example

When the user clicks the “Submit” button, the URL sent to the server could look something like this:

The “welcome.php” file can now use the $_GET function to collect form data (the names of the form fields will automatically be the keys in the $_GET array):

When to use method=”get”?
 When using method=”get” in HTML forms, all variable names and values are displayed in the URL.
Note: This method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page.This can be useful in some cases.
Note: The get method is not suitable for large variable values; the value cannot exceed 100 characters.The built-in $_POST function is used to collect values in a form with method=”post”.

ii.The $_POST Function

 The built-in $_POST function is used to collect values from a form sent with method=”post”.Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
Example

When the user clicks the “Submit” button, the URL will look like this:

The “welcome.php” file can now use the $_POST function to collect form data (the names of the form fields will automatically be the keys in the $_POST array):

WAP in php to take input from the user using form and calculate the factorial.

Change into UPPER or lower case

 

Super Global Variable

Some pre-defined variables are available in php which is know as superglobals variable, which means that they are always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.
List of superglobal variables are give below;

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

1. $GLOBALS variable

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). Using $GLOBALS[index]stores all global variables in an array. Here index hold all variable.

Example

 

2. $_SERVER Variable

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

Example

3.$_REQUEST Variable

PHP $_REQUEST is used to collect data after submitting an HTML form.

Example

4. $_POST Variable

PHP $_POST is widely used to collect form data after submitting an HTML form with method=”post”. $_POST is also widely used to pass variables.

 Example

5. $_FILES Variable

The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file name and errors associated with file.

Example

6. $_COOKIE Variable

$_COOKIE is used to create cookie.

Example

7. $_SESSION Variable

$_SESSION Variable is used to create session.

$_SESSION Example