Working with PHP forms (Get/Post) – FREE PHP TUTORIALS #2

php forms tutorials

We are back with our next tutorial as promised and today we’ll throw some light on how to build forms in HTML and get data through get and post method in php. The HTML <form> element is used to collect user input. Form makes use of different tags like <input> , <select> , <textarea> ,etc

READ MORE : Introduction to Web Development with PHP

Getting started with PHP Forms

Create a file ending with a .php extension. Start with <form> element first.

Action attribute specifies where to send the form data. Keep it blank for now. A form can constitute one or more of the following:

 
Input boxes:
Enter some text: 
Check boxes:
 Checked!
 Unchecked
Radio Buttons:

 Male
 Female

Submit Button

On clicking the submit button, the form send the data to the page specified in action attribute. Make sure you close the form tag

The type attribute specifies the type of <input> tag to display. The default type is: text. There are more than 20 type attribute values for the <input> tag.

Now that you have created the form it is time to get the value user will input in the form
This is done using the method attribute. The method attribute specifies how to send form-data. There are two ways of doing this:

get method:

  • Appends form-data into the URL (specified in action) in name=value pairs
  • The length of a URL is limited about 3k characters.
  • It must not be used to send important data as the data will be visible in the URL.

post method:

  • Appends form-data inside the body of the HTTP request (data is not shown is in URL)
  • It has no size limitations.
  • It is more secured.
Add this method=get” or method=”post” to the form element.

Now begins the php part. The PHP superglobals $_GET and $_POST are used to collect form-data using get and post method respectively. Superglobals are those variables, which have a global scope i.e. they are always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special. When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file mentioned in the action attribute. As we had kept it blank, so the form-data will be submitted to the same file.(php form )

Include the following code at the beginning of the file.

The output could be something like this:

You wrote  : Example_php_form

$_GET & $_POST  takes the name of input type as its parameter and it is case sensitive.

operator is used to concatenate two strings.
To get the data from the check boxes, add the following code in php.
Foreach loop is used to traverse the elements of array one by one from the beginning till the very last element. $c is an array here and $i is allocated the value of the next element in the array after each iteration.
Similarly for getting data from radio buttons.

If you get stuck anywhere, you can always use

to debug the code.

We’ll be up with our next tutorial very soon. Adios! 
Read this article offline by clicking the PDF icon above

2 Responses to “Working with PHP forms (Get/Post) – FREE PHP TUTORIALS #2

  • A couple of notes, szitinae your request variables before pulling data directing into you code. Avoid using double quote for simple string echo as it is extra load on php parsing use single quote instead. Play with MVC type frameworks such as Zends framework to get familiar with design patterns. Nice tutorial for beginners but be warned, I had to fire a bunch of people recently who programmed like this go back to programming basics and learn popular framework methodologies.

    • We have taken note of your suggestions. Corrections will be made. The tutorial was written to help beginners get started with php. Work on frameworks has already begun and will be published soon.

Leave a Reply

Your email address will not be published. Required fields are marked *