Menu

Create Simple Contact Form with CodeIgniter

Create Simple Contact Form with CodeIgniter

A contact page is a standard web page on a website used to allow the visitor to contact the Web Application admin.In this tutorial you will learn how to create CodeIgniter contact page with Bootstrap.The post covered in easy steps with live to create CodeIgniter contact page with Bootstrap.

Step 1: Load "email" class in controller.

$this->load->library('email');
?>


Step 2: Create a controller file
Next create a controller file named "Contact.php" inside "application/controllers" folder.


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* @package Contact : CodeIgniter Contact Form
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Contact Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Contact extends CI_Controller {

public function __construct() {
parent::__construct();
// load email lib
$this->load->library('email');
}
// contact
public function index() {
$data['page'] = 'contact';
$data['title'] = 'Contact Form | TechArise';
$this->load->view('contact/index', $data);
}

// send information
public function send() {
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('contact_no', 'Phone', 'trim|required');
$this->form_validation->set_rules('comment', 'Comments', 'trim|required');

if($this->form_validation->run() == FALSE) {
$this->index();
} else {
$name = $this->input->post('name');
$email = $this->input->post('email');
$contact_no = $this->input->post('contact_no');
$comment = $this->input->post('comment');
if(!empty($email)) {
// send mail
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$message='';
$bodyMsg = '

'.$comment.'

';
$delimeter = $name."
".$contact_no;
$dataMail = array('topMsg'=>'Hi Team', 'bodyMsg'=>$bodyMsg, 'thanksMsg'=>'Best regards,', 'delimeter'=> $delimeter);

$this->email->initialize($config);
$this->email->from($email, $name);
$this->email->to(TO_MAIL);
$this->email->subject('Contact Form');
$message = $this->load->view('mailTemplate/contactForm', $dataMail, TRUE);
$this->email->message($message);
$this->email->send();

// confirm mail
$bodyMsg = '

Thank you for contacting us.

';
$dataMail = array('topMsg'=>'Hi '.$name, 'bodyMsg'=>$bodyMsg, 'thanksMsg'=>'Best regards,', 'delimeter'=> 'Team TechArise');

$this->email->initialize($config);
$this->email->from(TO_MAIL, FROM_TEXT);
$this->email->to($email);
$this->email->subject('Contact Form Confimation');
$message = $this->load->view('mailTemplate/contactForm', $dataMail, TRUE);
$this->email->message($message);
$this->email->send();
}
$this->session->set_flashdata('msg', 'Thank you for your message. It has been sent.');
redirect('/');
}
}

}
?>


Step 3: Create a view
Create a view file named "index.php" inside "application/views/contact" folder


$this->load->view('templates/header');
?>


CodeIgniter Contact Form Example









session->flashdata('msg'))){ ?>

session->flashdata('msg'); ?>





























































$this->load->view('templates/footer');
?>


Step 4: Create a view(mailTemplate)
Create a view file named "contactForm.php" inside "application/views/mailTemplate" folder


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
?>























TECHARISE




,


















Demo  [sociallocker] Download[/sociallocker]

Ads middle content1

Ads middle content2