Sunday, 15 November 2015

How to create a contact form

This is view part:
<?php echo $this->session->flashdata('message');?>
      <form action="<?php echo base_url();?>contact" id="form1" name="form1" method="post" enctype="multipart/form-data">
        <div class="row">
          <div class="col-md-6 col-sm-6">
            <div class="form-group"> <span style="float:left;">&nbsp;
              <?=form_error('full_name');?>
              </span><br />
              <input type="text" name="full_name" id="full_name" class="form-control" value="<?=set_value('full_name');?>"   placeholder="Full Name"/>
            </div>
          </div>
          <div class="col-md-6 col-sm-6">
            <div class="form-group"> <span style="float:left;">&nbsp;
              <?=form_error('email');?>
              </span><br />
              <input type="text" name="email" id="email" class="form-control" value="<?=set_value('email');?>"   placeholder="Email address">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-md-12 col-sm-12">
            <div class="form-group"> <span style="float:left;">&nbsp;
              <?=form_error('message');?>
              </span><br />
              <textarea name="message" id="message"  class="form-control" rows="7" placeholder="Message"><?=set_value('message');?>
</textarea>
            </div>
            <div class="form-group">
              <input name="submit" type="submit" class="btn btn-primary" value="Submit Request"  />
            </div>
          </div>
        </div>
      </form>
*******************************************************************************
This is controller part:
 <?php defined('BASEPATH') OR exit('No direct script access allowed');

class Contact extends CI_Controller {
    function __construct() {
        parent::__construct();
        //Write the below code when you have not autoload below settings.
              
        //$this->load->helper(array('form', 'url', 'html'));
        //$this->load->library('form_validation');
      
        //If you have set in autoload then don't write below 2 commented lines.
      
        //If you want custom error message then write the below lines.
        $this->form_validation->set_message('alpha','Invalid Name');
        $this->form_validation->set_message('valid_email', 'Invalid Email Address');      
        $this->form_validation->set_message('required', 'Require');  
    }
  
    public function index()
    {
        if(!file_exists('application/views/contact.php')) { show_404(); }
        $data['title'] = "Contact us";
        $this->load->view('templates/header',$data);
        $this->load->view('templates/navigation');
        $this->load->view('contact');
        $this->load->view('templates/footer');
    }  
  
    public function contact(){
         $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

         $this->form_validation->set_rules('full_name', 'Full Name', 'trim|required|alpha|min_length[3]|max_length[15]');
         $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');        
         $this->form_validation->set_rules('message', 'Message', 'required');
        
         if ($this->form_validation->run() == FALSE)
            {
                $data['title'] = "Contact us";
                $this->load->view('templates/header',$data);
                $this->load->view('templates/navigation');
                $this->load->view('contact');
                $this->load->view('templates/footer');
            }           
        else
            {
                $this->load->model('contactform_model');
                $data['query'] = $this->contactform_model->form_insert();
            }  
    }
}


******************************************************************************* 
This is model part:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contactform_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();       
           
    }
   
    function form_insert(){
       
        $fullname = $this->input->post('full_name');
        $email = $this->input->post('email');   
        $message = $this->input->post('message');   
       
        $sql = "INSERT INTO tbl_contact(contact_fullname,contact_email,contact_message)
        VALUES ('$fullname','$email','$message')";               
       
        $result = $this->db->query($sql);   
        if($this->db->affected_rows()>0){
            $this->session->set_flashdata('message', '   
            <div class="alert alert-success alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>Successfully added</div>'); 
           redirect('contact','refresh');  
       }
       else{
           $this->session->set_flashdata('message', '          
           <div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
Cannot be added</div>          
           ');
           redirect('contact','refresh');  
       }
    }
}   

No comments:

Post a Comment