Last active
February 25, 2018 04:46
-
-
Save rintoug/42a6dfc124ab5c7cdb337fe272309f7f to your computer and use it in GitHub Desktop.
How to Create a Contact Form In CodeIgniter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class Contact extends CI_Controller { | |
/** | |
* Index Page for this controller. | |
* | |
* Maps to the following URL | |
* http://example.com/index.php/welcome | |
* - or - | |
* http://example.com/index.php/welcome/index | |
* - or - | |
* Since this controller is set as the default controller in | |
* config/routes.php, it's displayed at http://example.com/ | |
* | |
* So any other public methods not prefixed with an underscore will | |
* map to /index.php/welcome/<method_name> | |
* @see https://codeigniter.com/user_guide/general/urls.html | |
*/ | |
public function index() | |
{ | |
//Loading the form validation library | |
$this->load->library(array('form_validation','session')); | |
$this->form_validation->set_rules('firstname', 'Firstname', 'required'); | |
$this->form_validation->set_rules('lastname', 'Lastname', 'required'); | |
$this->form_validation->set_rules('email', 'Email', 'required'); | |
$this->form_validation->set_rules('telephone', 'Telephone', 'required'); | |
$this->form_validation->set_rules('comment', 'Comment', 'required'); | |
if ($this->form_validation->run() == TRUE) { | |
$this->load->library('email'); | |
$this->email->from('[email protected]', 'John Doe'); | |
$this->email->to('[email protected]'); | |
$this->email->cc('[email protected]'); | |
$this->email->subject('Contact Form'); | |
$message = 'Name:'.$this->input->post("firstname"); | |
$message .= "<br>"; | |
$message .= 'Email:'.$this->input->post("email"); | |
$message .= "<br>"; | |
$message .= 'Comment:'.$this->input->post("comment"); | |
$this->email->message($message); | |
$this->email->send(); | |
$this->session->set_flashdata('msg', 'Your Contact request sent!'); | |
} | |
$this->load->view('contact'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment