PHP Forms- Tutorialpath

What is Form?

PHP form is used to take input from users. in PHP if you want to take input from keyboard and display the output according to input, in that case you have to use html form.

The diagram below illustrates the form handling process.

Create a form

We will use HTML tags to create a form. Below is the minimal list of things you need to create a form.

  • Opening and closing form tags <form>…</form>
  • Form submission type POST or GET
  • Submission URL that will process the submitted data
  • Input fields such as input boxes, text areas, buttons,checkboxes etc.

PHP Form Handling

We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET and $_POST.

The form request may be get or post. To retrieve data from get request, we need to use $_GET, for post request $_POST.

PHP GET method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP GET method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method displays the form values in the URL.
  • It’s ideal for search engine forms as it allows the users to book mark the results.

Let’s see a simple example to receive data from get request in PHP:

 

Output:

File: welcome.php

 

PHP Post Form

  • Post request is widely used to submit form that have large amount of data such as file upload, image upload, login form, registration form etc.
  • This is the built in PHP super global array variable that is used to get values submitted via HTTP POST method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method is ideal when you do not want to display the form post values in the URL.
  • A good example of using post method is when submitting login details to the server.

Example:

Output:

 

 

Leave a Reply

Your email address will not be published.