- Programming with CodeIgniterMVC
- Eli Orr Yehuda Zadik
- 398字
- 2021-08-06 16:48:04
Example 4 – interactive contact forms
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 file
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
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>
- Vue.js 3.x快速入門
- LabVIEW 2018 虛擬儀器程序設(shè)計
- Microsoft Dynamics 365 Extensions Cookbook
- Learning C++ Functional Programming
- Python從入門到精通(精粹版)
- SQL語言從入門到精通
- Python Tools for Visual Studio
- 匯編語言程序設(shè)計(第3版)
- Visual Basic程序設(shè)計上機實驗教程
- 區(qū)塊鏈技術(shù)進階與實戰(zhàn)(第2版)
- Hands-On Nuxt.js Web Development
- AI自動化測試:技術(shù)原理、平臺搭建與工程實踐
- UI動效設(shè)計從入門到精通
- Mastering Unity Scripting
- HTML5 and CSS3:Building Responsive Websites