Create contact forms in codeIgniter

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 file

The controller creates a form for adding and editing a product.

 

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

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>

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

  • bald

    Hi. I tried your code and it shows me :

    An uncaught Exception was encountered

    Type: ParseError
    Message: syntax error, unexpected ‘$index’ (T_VARIABLE), expecting ‘;’
    Filename: /sport/application/controllers/Contact.php
    Line Number: 34

    Backtrace:
    File: /sport/index.php
    Line: 315
    Function: require_once

    Can you help me please ? :c

Pin It on Pinterest

Shares

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)