[eluser]Tekamba[/eluser]
------------------------------------------------------
views/contact.php (excerpt)
------------------------------------------------------
Code:
<div id="contact-form">
<h2>Contact Us</h2>
<?php
if ($this->session->flashdata('errors') != '') {
echo "<div class=\"alert\">";
echo $this->session->flashdata('errors');
echo "</div>";
}
if ($this->session->flashdata('messagestatus') != '') {
echo "<div class=\"notification\">";
echo $this->session->flashdata('messagestatus');
echo "</div>";
}
echo form_open('email/contact_send');
echo form_hidden('formname', 'Contact_Us Form');
echo form_label('Name', 'name');
echo form_input('name', $this->input->post('name'));
echo form_label('Phone', 'phone');
echo form_input('phone', $this->input->post('phone'));
echo form_label('Email', 'email');
echo form_input('email', $this->input->post('email'));
echo form_label('Reason', 'reason');
$data = array(
'name' => 'reason',
'rows' => '6',
'cols' => '35'
);
echo form_textarea($data, $this->input->post('reason'));
echo form_submit('submit', 'Send Request');
echo form_close();
?>
</div> <!-- end contact-form -->
------------------------------------------------------
controller/email.php (excerpt)
------------------------------------------------------
Code:
function contact_send() {
/*******************************************/
/* The Contact Form was submitted */
/*******************************************/
// load the form validation library
$this->load->library('form_validation');
//setup validation rules on form fields
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('phone', 'Phone', 'trim|required');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('reason', 'Reason for contact request', 'trim|required|');
// run form validation
if($this->form_validation->run() == TRUE)
{
// validator has passed - compile the email
$messagebody = "Name: ";
$messagebody .= $this->input->post('name');
$messagebody .= "<br>Phone: ";
$messagebody .= $this->input->post('phone');
$messagebody .= "<br>E-Mail: ";
$messagebody .= $this->input->post('email');
$messagebody .= "<br>Reason: ";
$messagebody .= $this->input->post('reason');
$msg_subject = 'Contact Form Request';
$msg_valid_send = 'Thank you for your contact request';
$msg_error_send = 'Contact form message did not process properly';
/********************************************/
/* Validation&Message; construction */
/* are done so send the message */
/********************************************/
// create array to store smtp values
$config = Array(
'protocol' => $this->config->item('smtp_protocol'),
'smtp_host' => $this->config->item('smtp_host'),
'smtp_port' => $this->config->item('smtp_port'),
'smtp_user' => $this->config->item('smtp_user'),
'smtp_pass' => $this->config->item('smtp_pass'),
'mailtype' => $this->config->item('smtp_mailtype'),
'charset' => $this->config->item('smtp_charset'),
'recipient' => $this->config->item('smtp_recipient'));
// load the email library class
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
// set to, from, message, etc
$this->email->from($this->input->post('email'));
$this->email->to($config['recipient']);
$this->email->subject($msg_subject);
$this->email->message($messagebody);
// attempt to send the email
if ($result = $this->email->send()) {
$message = $msg_valid_send;
} else {
$message = $msg_error_send;
}
$this->session->set_flashdata('messagestatus', $message);
// send user back to the appropriate form
$this->load->view('includes/template');
} else {
// form failed to validate so pass the errors
// to flashdata and then return user to form
$this->session->set_flashdata('errors', validation_errors());
$this->load->view('includes/template');
}
}
------------------------------------------------------
views/includes/template.php (complete)
NOTE: variables are stored in session which will return the user to the last page they were on (main_content variable), the variable is adusted in the main controller function calls for page navigation
------------------------------------------------------
Code:
<?php
$this->load->view('includes/startpage');
$this->load->view('includes/header');
?>
<div id="main_content">
<?php $this->load->view($this->session->userdata('main_content')); ?>
</div>
<?php
$this->load->view('includes/footer');
$this->load->view('includes/endpage');
?>