How to? - Process a form using PHP (procedural style)

Posted on July 7, 2014, 10:40 pm by about-dev.com


Today I'll show you how can you use the information that a user sends you using a HTML form. Let's suppose we have a registration form and we want to save the data into a users table with the following structure: id, first_name, last_name, email, username, password, address, date_inserted, date_modified.

Step 1create the database design
a) create the database (I named it "test")
c) create the users table

Step 2: create the project structure
/form_application/index.php          --> this file contains the HTML form
/form_application/functions.php    --> this file contains some user defined functions (for example a validation function)

Here, you can add any CSS or JavaScript code you want. In the archive from Github you'll see that I did this. 

Step 3: create the HTML form and save it into a file (a PHP file!, because we want to perform some PHP code on it)

You can see the form code here.

Step 4: create the code that processes the information from it; we want it to be saved into our users table
a) decide what HTTP method will you use to process the form: I'll use GET (the other option is POST) ==> put the attribute method="get"
b) decide who will take care of the prcessing part: I'll do the processing in the same file ==> put the attribute action=""
c) decide if you'll organize your code into user-defined functions or not: I'll do that ==> you'll see in the following code section that I include a functions.php file (this is the file that keeps my two functions, validation() and save())

You can see the functions.php code by clicking this link.

d) write the processing code and don't forget to take care of the errors too

       if(!empty($_POST)){
           require_once('functions.php');

            $response = validation($_POST);
            if(!empty($response['status']) && $response['status'] == 'ok'){
                $response = save($_POST);
            }
        }
if(!empty($response['status']) && $response['status'] == 'error'):
      //here you can use any HTML tag you want for error displaying
      if(is_array($response['message'])):
               foreach($response['message'] as $val):
                       echo $val;
                endforeach;
      endif;
elseif(!empty($response['status']) && $response['status'] == 'ok'):
      if(!empty($response['message']['success'])):?>
                echo $response['message']['success'];
                $_POST = array();
      endif;
endif;

Step 5: test the code
Now we have to test our code, to see if the form will be validated and if the information goes where we wanted to: into our users table.
Type the following URL into your client: http://localhost/form_application    (this will point to the index.php file which keeps your form)

You'll be able to fill the form with some values and test the validation and the saving part.

Stay tune for the OOP style and finally the "framework" style.

Download source fileshttps://github.com/about-dev/php_form_processing_procedural

Related articles:


Leave a Comment:

User
Email
Website

Blog Search

Popular Blog Categories

Newsletter

Want to be informed about latest posts? Subscribe to our newsletter