Learn PHP from Scratch Day -10

Learn PHP from Scratch Day -10
Learn PHP from Scratch Day -10

Today we are going to see what are forms and how we can process data on submission of the form. Let’s consider a registration form to which we everyone familiar, In the HTML we create form using the

tag. There are many attributes with the form tag , one of them is method , this attribute defines the data transmission method from this form to the respected action url. The possible values for this method attribute are

POST

GET

PUT

DELETE

  • The current HTML5 draft does not support PUT or DELETE in forms. The relevant section is currently in at "last call for comments", so it's possible they'll be implemented but unfortunately it doesn't look like it. I think it was in there at one point and then removed. (Source : Stackoverflow )
  • We only consider POST & GET methods for submitting the data for while , other methods DELETE & PUT are used with REST API’s.

WHEN & WHY WE SHOULD USE THE FORM ?

There are two general purposes of form a) Accepts the user inputs and process them for further use or create data functionality. b) Editing the existing user input i.e. edit data functionality.

The code below creates a simple registration form

        
            
            

Registration Form

First name:
Last name:
        
        

HERE,

  • <form…>… are the opening and closing form tags
  • action="registration_form.php" method="POST"> specifies the destination URL and the submission type.
  • First/Last name: are labels for the input boxes

  • is the new line tag
  • is a hidden value that is used to check whether the form has been submitted or not
  • is the button that when clicked submits the form to the server for processing.

Submitting the form data to the server

The action attribute of the form specifies the submission URL that processes the data. The method attribute specifies the submission type.

PHP POST method

  • 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.

It has the following syntax.

HERE,

  • "$_POST[…]" is the PHP array
  • "‘variable_name’" is the URL variable name.

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.

It has the following syntax.

HERE,

  • "$_GET[…]" is the PHP array
  • "‘variable_name’" is the URL variable name.

GET VS POST

1

GET VS POST