How get data from form submit in php?


The PHP superglobals $_GET and $_POST are used to collect form-data.


PHP - A Simple HTML Form

The example below displays a simple HTML form with two input fields and a submit button:

Example



Name:

E-mail:



Run Example »

When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.

To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:


Welcome

Your email address is:


The output could be something like this:

Welcome John
Your email address is

The same result could also be achieved using the HTTP GET method:

Example



Name:

E-mail:



Run Example »

and "welcome_get.php" looks like this:


Welcome

Your email address is:


The code above is quite simple. However, the most important thing is missing. You need to validate form data to protect your script from malicious code.

Think SECURITY when processing PHP forms!

This page does not contain any form validation, it just shows how you can send and retrieve form data.

However, the next pages will show how to process PHP forms with security in mind! Proper validation of form data is important to protect your form from hackers and spammers!



GET vs. POST

Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

Both GET and POST are treated as $_GET and $_POST. These are superglobals, 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.

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method.


When to use GET?

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

GET may be used for sending non-sensitive data.

Note: GET should NEVER be used for sending passwords or other sensitive information!


When to use POST?

Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

Developers prefer POST for sending form data.

Next, lets see how we can process PHP forms the secure way!


PHP Exercises

Test Yourself With Exercises

Exercise:

If the form in the white section below gets submitted, how can you, in welcome.php, output the value from the "first name" field?

First name:

Welcome




PHP Form Handling

In this tutorial you'll learn how to collect user inputs submitted through a form using the PHP superglobal variables $_GET, $_POST and $_REQUEST.

Creating a Simple Contact Form

In this tutorial we are going to create a simple HMTL contact form that allows users to enter their comment and feedback then displays it to the browser using PHP.

Open up your favorite code editor and create a new PHP file. Now type the following code and save this file as "contact-form.php" in the root directory of your project.




    
    Contact Form


    

Contact Us

Please fill in this form and send us.

Explanation of code

Notice that there are two attributes within the opening

tag:

  • The action attribute references a PHP file "process-form.php" that receives the data entered into the form when user submit it by pressing the submit button.
  • The method attribute tells the browser to send the form data through POST method.

Rest of the elements inside the form are basic form controls to receive user inputs. To learn more about HTML form elements please check out the HTML Forms tutorial.


Capturing Form Data with PHP

To access the value of a particular form field, you can use the following superglobal variables. These variables are available in all scopes throughout a script.

SuperglobalDescription
$_GET Contains a list of all the field names and values sent by a form using the get method (i.e. via the URL parameters).
$_POST Contains a list of all the field names and values sent by a form using the post method (data will not visible in the URL).
$_REQUEST Contains the values of both the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variable.

When a user submit the above contact form through clicking the submit button, the form data is sent to the "process-form.php" file on the server for processing. It simply captures the information submitted by the user and displays it to browser.

The PHP code of "process-form.php" file will look something like this:




    
    Contact Form


    

Thank You

Here is the information you have submitted:

  1. Name:
  2. Email:
  3. Subject:
  4. Message:

The PHP code above is quite simple. Since the form data is sent through the post method, you can retrieve the value of a particular form field by passing its name to the $_POST superglobal array, and displays each field value using echo() statement.

In real world you cannot trust the user inputs; you must implement some sort of validation to filter the user inputs before using them. In the next chapter you will learn how sanitize and validate this contact form data and send it through the email using PHP.

How can I get submitted form value in PHP?

Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method.

How will you retrieve values submitted by this form?

Since the form data is sent through the post method, you can retrieve the value of a particular form field by passing its name to the $_POST superglobal array, and displays each field value using echo() statement.

Can PHP collect form data?

The PHP superglobals $_GET and $_POST are used to collect form-data.

How get data from form and save it to database in PHP?

php Save both files in one folder under htdocs..
Start XAMPP Server..
Open localhost/phpmyadmin in your web browser..
Create database of name staff and table of name college..
Write HTML and PHP code in your Notepad in a particular folder..
Submit data through HTML Form..
Verify the results..