Essentially, the PHP application that I plan to build now will perform really basic tasks, such as sending email by utilizing an HTML form. However, the most interesting facet in developing such a program is that I’m going to combine the functionality of two classes of Code Igniter: the one that sends email, and another one that validates input data. In addition, I’m going to use the “url” and “form” helper functions that you learned in previous tutorials to dynamically generate certain links. So, having explained how this sample email application is going to work, it’s time to create its first building block, the controller class, whose signature is shown below: class Email extends Controller { function Email(){ // load controller parent parent::Controller(); // load 'url' helper $this->load->helper('url'); // load 'form' helper $this->load->helper('form'); // load 'validation' class $this->load->library('validation'); // load 'email' class $this->load->library('email'); } function index(){ // set validation rules $rules['firstname']="required|min_length[6]|max_length[15]"; $rules['lastname']="required|min_length[6]|max_length[15]"; $rules['email']="required|valid_email"; $this->validation->set_rules($rules); // set values to repopulate fields $fields['firstname']='First Name'; $fields['lastname']= 'Last Name'; $fields['email']='Email Address'; $this->validation->set_fields($fields); // check if user form has been submitted properly if ($this->validation->run()==FALSE){ // redisplay user form and repopulate fields $this->load->view('form_view'); } // display confirmation web page and send email else{ // set email class settings $this->email->from($_POST['email'],$_POST['firstname']. ' '.$_POST['lastname']); $this->email->to('you@domain.com'); $this->email->cc('migirlfriend@domain.com'); $this->email->bcc('myothergirlfriend@domain.com'); $this->email->subject($_POST['subject']); $this->email->message($_POST['message']); $data['title']='Sending email...'; $data['header']='Sending email now...'; $data['message']=$this->email->send()?'Message was sent successfully!':'Error sending email!'; $this->load->view('email_view.php',$data); } } } Actually, I don’t wan to be excessively optimistic, but in this case the logic implemented by the above “Email” controller is very easy to grasp. As you can see, its constructor begins loading all of the source classes and functions that will be required later on, and this naturally includes all of the classes and helpers that were mentioned previously. Now, focusing on the implementation of the “index()” method, it’s clear to see that it first sets the validation rules that will be applied to the data entered in the HTML form, then validates this incoming information, and finally submits it via email, assuming that it’s been filled in correctly. Otherwise, the online form will be redisplayed, indicating which entries should be corrected. As you’ll possibly agree with me, understanding how the above controller class work isn’t something that will make you scratch your head, is it? However, there’s still one step that remains undone if we want to get this email application working as expected. Yes, you guessed right! In this case, it’s necessary to create the view file that informs users that the HTML form has been submitted successfully. We also need to code the online form that lets them enter their personal information. So, taking into account those last requirements, here’s the signature of these simple view files: (definition of 'email_view.php' file) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title><?php echo $title;?>title> </head> <body> <h1><?php echo $header;?></h1> <p><?php echo $message;?></p> <p><?php echo anchor('email','Go and submit the form again');?></p> </form> </body> </html> (definition of 'form_view.php' file) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Entering user data</title> <head> <body> <h1>Entering user data</h1> <?php echo $this->validation->error_string;?> <?php echo form_open('email');?> <p>First Name <input type="text" name="firstname" value="<?php echo $this->validation->firstname;?>" size="50" /></p> <p>Last Name <input type="text" name="lastname" value="<?php echo $this->validation->lastname;?>" size="50" /></p> <p>Email <input type="text" name="email" value="<?php echo $this->validation->email;?>" size="50" /></p> <p><input type="submit" value="Send Data" /></p> </form> </body> </html> Here you have it. Assuming that the controller class and the respective view files have been saved to the correct destination folders, the previous email application now should work like a charm. Of course, as I explained in preceding articles, you can test it by typing into your browse the following URL: http://localhost/codeigniter/index.php/email/ And with this last hands-on example, I’m finishing this overview of building an email application with Code Igniter. As always, feel free to tweak the entirety of the code samples included in this article, so you can improve your skills in using this powerful PHP framework. Final thoughts In this eighth part of the series, I explained how to build a web application with Code Igniter that permits users to send email via a simple HTML form. Although this example may seem pretty simplistic, it hopefully will be useful enough to demonstrate how to combine several classes and helpers into one single program. The last article will be the fruit of the cake, since I’ll be showing you how to use Code Igniter to develop a content management system that will permit you to insert, update and delete information about a collection of movies. Yes, this time we’re going to Hollywood, so don’t miss the last article!
blog comments powered by Disqus |
|
|
|
|
|
|
|