FuelPHP - Form Programming


Advertisements


FuelPHP provides three classes, Form Fieldset,, and Input,, to perform Form programming.

  • Form class provides an option to create all HTML forms elements.

  • Fieldset class provides an option to create html element through higher level methods, integrating the models and validation.

  • Input class provides an option to parse the data submitted through html forms as well as http parameters, server variables, and user agents.

In this chapter, let us learn Form programming in FuelPHP.

Form

As discussed earlier, Form class provides methods to create html form elements and the important methods are as follows −

open()

open() is used to create a new form. It provides the following two parameters −

  • $attributes − attributes of form tag as array or just the action URL as string.

  • $hidden − array of hidden field name and their values.

echo Form::open('/employee/add'); 
echo Form::open(array('action' => '/employee/add', 'method' => 'post'));

close()

close() simply closes the form.

echo Form::close();

input()

input() creates html input element. It has the following three parameters,

  • $field − name of the input element

  • $value − value of the input element

  • $attributes − attributes of input element as array

echo Form::input('name', 'jon', array('style' => 'border: 20px;'));

label element

label creates html label element. It has the following three parameters,

  • $label − label to show

  • $id − associated form element id

  • $attributes − attributes of label element as array

echo Form::label('Employee Name', 'employee_name');

hidden

hidden is similar to input method, except it sets the type of the input element to hidden.

password

password is similar to input method, except it sets the type of the input element to password.

radio

radio is similar to input method, except it sets the type of the input element to radio. It has the following four parameters,

  • $field − name of the input element

  • $value − value of the input element

  • $checked − whether the item is checked or not (true / false)

  • $attributes − attributes of input element as array

echo Form::label('Male', 'gender'); 
echo Form::radio('gender', 'Male', true); 
echo Form::label('Female', 'gender'); 
echo Form::radio('gender', 'Female');

checkbox

checkbox is similar to input method, except it sets the type of the input element to checkbox. It has the following four parameters,

  • $field − name of the input element

  • $value − value of the input element

  • $checked − whether the item is checked or not (true / false)

  • $attributes − attributes of input element as array

echo Form::label('Male', 'gender'); 
echo Form::checkbox('gender', 'Male', true);
echo Form::label('Female', 'gender'); 
echo Form::checkbox('gender', 'Female');

file

file is similar to input method, except it sets the type of the input element to file.

textarea

textarea creates html textarea element. It has the following three parameters,

  • $field − name of the textarea element

  • $value − value of the textarea element

  • $attributes − attributes of textarea element as array

echo Form::textarea ('description', 'original data (value)', array ('rows' => 6, 
      'cols' => 8)); 

select

select creates an HTML select element. It has the following four parameters −

  • $field − name of the select element

  • $values − initial selection values

  • $options − options as array. Options may be grouped using nested array

  • $attributes − attributes of input element as array

echo Form::select ( 
   'country',  
   'none',  
   array ( 
      'none'  => 'None', 
      'asia'  => array ( 
         'in' > 'India', 
         'cn' => 'China' 
      ), 
      
      'us' => 'United States' 
   ) 
);

submit

submit is similar to input method, except it sets the type of the input element to submit.

button

button creates html button element. It has the following three parameters,

  • $field − name of the button element

  • $value − value of the button element

  • $attributes − attributes of button element as array

echo Form::button('emp_submit', 'Submit');

reset

reset is similar to input method, except it sets the type of the input element to reset.

fieldset_open

fieldset_open creates html field set and legend elements. It has the following two parameters −

  • attributes − attributes of fieldset element as array

  • legend − name of the legend to create

// returns <fieldset class = "example-class" id = "example-id">
<legend>
   Custom Legend
</legend> 

echo Form::fieldset_open (array (
   'class'  => 'example-class', 
   'id'     => 'exampleid', 
   'legend' => 'Custom Legend'
));

fieldset_close

fieldset_close creates the HTML field set close tag.

// returns </fieldset> 
echo Form::fieldset_close(); 

Input Class

Input class provides methods to read all request data along with form details. Some of the important methods are as follows −

uri

uri returns the current URI of the request

// request: http://localhost:8080/employee/welcome  
echo Input::uri(); // return /employee/welcome

method

method returns the HTTP method used in the request

echo Input::method() // "POST"

get

get enables to read $_GET variables. It has the following two parameters,

  • $index − index of the $_GET array

  • $default − default value, if index is not found.

echo Input::get('age', '20'); // returns $_GET['age']

post

post enables to read $_POST variables. It has the following two parameters,

  • $index − index of the $_POST array

  • $default − default value, if index is not found

echo Input::get('age', '20'); // returns $_POST['age']

param

param enables to fetch item from either $_GET, $_POST, $_PUT, or $_DELETE variables. It has the following two parameters,

  • $index − index of the array

  • $default − default value, if index is not found

If no parameter is specified, it will return all items.

echo Input::param('age', '20'); // returns $_POST['age']

file

file enables to read $_FILE variables. It has the following two parameters,

  • $index − index of the $_POST array

  • $default − default value, if index is not found

echo Input::file();

is_ajax

is_ajax returns true, if the request is made through AJAX.

echo Input::is_ajax() // return false

protocol

protocol returns the HTTP protocol used in the request.

echo Input::protocol() // returns "HTTP"

ip

ip returns the IP address through which the request is made.

echo Input::ip() // returns "84.45.34.24" (Public IP Address)

real_ip

real_ip tries to return the real IP address (if the client is behind proxy) through which the request is made.

echo Input::real_ip() // returns "10.76.12.1" (local private IP Address)

server

server enables to read $_SERVER variables. It has the following two parameters,

  • $index − index of the $_POST array

  • $default − default value, if index is not found.

echo Input::server('HTTP_HOST'); // returns localhost:8080

referrer

referrer returns the referrer from $_SERVER variable. It is a shortcut method to get the http referrer of the current request.

user_agent

user_agent returns the user agent from $_SERVER variable. It is a shortcut method to get the http user agent of the current request.

query_string

query_string returns the query string from $_SERVER variable. It is a shortcut method to get the query string of the current request.

headers

headers return the specific or all headers. It has the following two parameters −

  • $index − name of the HTTP headers

  • $default − default value, if index is not found.

echo Input::headers('Content-Type'); // returns "text/html"

extension

extension returns the URI extension of the current request.

// Example URL: http://localhost/test/ 
echo Input::extension();  // NULL  

// Example URL: http://localhost/test.html 
echo Input::extension();  // 'html'

Working Example

Let’s create a simple form to add new employee using Form and Input class.

Create form

Create new action, get_add in the employee controller as follows.

public function get_add() { 
   return Response::forge(View::forge('employee/add')); 
} 

Now, add view for the action, fuel/app/views/employee/add.php as follows.

<!DOCTYPE html> 
<html lang = "en"> 
   <head> 
      <title>Employee :: add page</title> 
      <meta charset = "utf-8"> 
      <meta name = "viewport" content = "width = device-width, initial-scale = 1"> 
      <?php echo Asset::css('bootstrap.css'); ?> 
   </head>
   
   <body> 
      <div class = "container"> 
         <?php 
            echo Form::open(array('action' => 'employee/add', 'method' => 'post')); 
         ?>  
         
         <div class = "form-group"> 
            <?php 
               echo Form::label('Employee name:', 'name'); 
               echo Form::input('name', '', array('class' => 'form-control')); 
            ?> 
         </div> 
         
         <div class = "form-group"> 
            <?php 
               echo Form::label('Employee age:', 'age'); 
               echo Form::input('age', '', array('class' => 'form-control')); 
            ?> 
         </div> 
         
         <?php echo Form::button('frmbutton', 'Submit', array(
            'class' => 'btn btn-default')); 
         ?> 
         
         <?php 
            echo Form::close(); 
         ?> 
      </div> 
   </body> 
   
</html>

Here, we have used bootstrap to design the form. FuelPHP provides full support for bootstrap components. Now, requesting the page, http://localhost:8080/employee/add will show the following form.

Form Design

Process Form

Create new action, post_add to process the form and add the employee data entered by the user into the database in the employee controller as follows.

public function post_add() { 
   $name = Input::post('name'); 
   $age = Input::post('age'); 
   $model = new model_employee(); 
   $model->name = $name; 
   $model->age = $age; 
   $model->save();  
   Response::redirect('employee/list'); 
}

Here, we have been redirected to employee list page, once the user entered data is saved into the database. Next, we will create the employee list page.

List Employee

Create new action, action_list to list the employee in the database as follows.

public function action_list() { 
   $data = array(); 
   $data['emps'] = model_employee::find('all');
   return Response::forge(view::forge('employee/list', $data)); 
}

Create new view, fuel/app/views/employee/list for the above action as follows.

<ul> 
   <?php 
      foreach($emps as $emp) {  
   ?> 
   <li><?php echo $emp['name']; ?></li> 
   <?php 
   } 
   ?> 
</ul> 

Check the Form

Now, request the URL, http://localhost:8080/employee/add, enter some employee data as shown in the following screenshot and submit the form.

Employee Data

Then, it shows all the employees (including newly added one) available in the database as follows −

Database

Advertisements