-
Book Overview & Buying
-
Table Of Contents
Programming with CodeIgniter MVC
This example shows how to write a contact form using the CI form helper and the form_validation library.
For more information about libraries, refer to Chapter 4, Libraries, and for information about helpers, refer to Chapter 5, Helpers.
The CI controller defines a form validation setup using the form_validation library and renders a form view that uses the form_validation library setup to apply a desired validation on the submitted data by the user. If it's a success, the CI controller will render a view page displaying a success message, otherwise it will render the view page with the form and the error messages will be displayed.
The URI for this example is http://ourdomain.com/index.php/contact.
In order to perform this example, we shall build the following three scripts:
The contact form controller class: application/controllers/contact.php
The view form script: application/views/contactview.php
The view success page script: application/views/contactsuccess.php
The controller creates a form for adding and editing a product.
For more information, refer to Chapter 7, Views.
The following is the code sample of the controller:
<?php
class Contact extends CI_Controller {
public function index()
{
//Loading the form helper
$this->load->helper('form');
//Loading the form_validation library
$this->load->library('form_validation');
$view_params['form']['attributes'] = array('id' =>'myform');
//contact name details
$view_params['form']['contact_name']['label'] = array('text' => 'Your name:', 'for' => 'name');
$view_params['form']['contact_name']['field']= array('name' => 'contact_name', 'id' => 'contact_name','value'=>isset($_POST['contact_name']) ?
$_POST['contact_name'] : '',
'maxlength' => '100', 'size' => '30', 'class' => 'input');
//contact name details
$view_params['form']['contact_email']['label'] = array('text' => 'Your email:', 'for' => 'email');
$view_params['form']['contact_email']['field'] = array('name' => 'contact_email', 'id' => 'contact_email','value'=> isset($_POST['contact_email']) ?
$_POST['contact_email'] : '',
'maxlength' => '100', 'size' => '30', 'class' => 'input');
//contact message details
$view_params['form']['contact_message']['label'] = array('text' => 'Your message:', 'for' => 'message');
$view_params['form']['contact_message']['field'] = array('name' => 'contact_message', 'id' => 'contact_message','value' => isset($_POST['contact_message']) ?
$_POST['contact_message'] : '',
'rows' => '10', 'cols' => '100', 'class' => 'input');
// Setting validation rules
$config_rules = array(array('field' => 'contact_name','label' => 'Contact Name', 'rules' => 'trim|required'),
array('field' => 'contact_email', 'label' => 'Contact Email','rules' => 'trim|required|valid_email'));
$this->form_validation->set_rules($config_rules);
$this->form_validation->set_rules('contact_message','Contact Message', 'trim|required');
// Validating the form
if ($this->form_validation->run() == FALSE)
// failed
{
for ($index = 0; $index < count($a_fields) $index++);
{
$s_field = $a_fields[$index];
if (form_error($s_field))
{
$view_params['form'][$s_field]['field']['class'] .= 'error';
}
}
$this->load->view('contactview', $view_params);
}
else // Validation succeeded
{
$success_params = array('message'=> 'Success');
$this->load->view('contactsuccess', $success_params);
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */The view file displays the contact form for receiving data from the user.
The following is the corresponding rendered form view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php if (validation_errors()) : ?>
<?php echo validation_errors() ; ?>
<?php endif; ?>
<?php echo form_open('contact', $form['attributes']) ; ?>
<table>
<tr>
<td><?php echo form_label($form['contact_name']['label']['text'],
$form['contact_name']['label']['for']);?>
</td>
<td><?php echo form_input($form['contact_name']['field']); ?></td>
</tr>
<tr>
<td><?php echo form_label($form['contact_email']['label']['text'],
$form['contact_email']['label']['for']);?>
</td>
<td><?php echo form_input($form['contact_email']['field']);?>
</td>
</tr>
<tr>
<td><?php echo
form_label($form['contact_message']['label']['text'],
$form['contact_message']['label']['for']); ?>
</td>
<td><?php echo form_textarea($form['contact_message']['field']);?>
</td>
</tr>
<tr>
<td colspan="3"><?php echo form_submit('mysubmit', 'Send'); ?></td>
</tr>
</table>
<?php echo form_close() ; ?>
</body>
</html>
The following is the corresponding rendered success view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact sent</title>
</head>
<body>
<div id="container">
<div id="body">
<p><?php echo $message ?></p>
</div>
</div>
</body>
</html>Change the font size
Change margin width
Change background colour