EllisLab text mark
Advanced Search
     
Codeigniter contact form - newbie
Posted: 05 February 2013 03:25 PM
Joined: 2013-02-05
7 posts

I’m brand new to code igniter and learning it as I’m working on this website
Basically I’m trying to get a contact form going but it’s proving to be VERY difficult for some reason

I have the form displaying on my site and everything but when I submit it it goes to a blank page and doesn’t send the email or go to success_view like it should

the page is:  http://www.vancouverticket.com/contact then it goes to contact/emailsender instead of running the email sender function

I’m sure I’m missing something silly but please help me out
I got the original code from:  https://github.com/furiston/codeigniter-contact-form/pull/1/files and modified the code a bit

here is the controller:

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

class 
Contact extends CI_Controller{
    
function __construct() {
        parent
::__construct();
    
}
    
    
function index(){
       
 
// load email and form helpers ** you can autoload them from config/autoload.php
 
$this->load->helper('email');
 
$this->load->helper('form'); 

   
   
$this->load->model('Testimonials_model');
  
$navigation_data['testimonial'$this->Testimonials_model->show_random();
  
  
$header_data['page_title']   'Contact Vancouver Ticket';
  
$navigation_data['active_nav']  'contact';
  
  
$this->load->view('templates/header'$header_data);
  
$this->load->view('templates/left_navigation_view'$navigation_data);
  
$this->load->view('contact_view');
  
$this->load->view('templates/footer');
    
}

    
function emailsender(){
 
// load form validation class
     
$this->load->library('form_validation');

 
// set form validation rules
     
$this->form_validation->set_rules('sender_name''From''required');
 
$this->form_validation->set_rules('sender_email''Email''required|valid_email');
 
$this->form_validation->set_rules('subject''Subject''required');
 
$this->form_validation->set_rules('message''Message''required');

     if (
$this->form_validation->run() == FALSE)
 
{
  $this
->load->view('contact_view');
 
}
 
else {
  
// you can also load email library here
  // $this->load->library('email');

  // set email data
      
$this->email->from($this->input->post('sender_email'), $this->input->post('sender_name'));
      
$this->email->to('sales@vancouverticket.com');
      
$this->email->reply_to($this->input->post('sender_email'), $this->input->post('sender_name'));
      
$this->email->subject($this->input->post('subject'));
      
$this->email->message($this->input->post('message'));
      
$this->email->send();

  
// create a view named "succes_view" and load it at the end
      
$this->load->view('success_view');
 
}     
    }
}
?> 


and the view

<head>
 <
meta charset="utf-8">
 <
title>Contact Us</title>
 <
meta name="description" c Vancouver Ticket and Tour Service at sales@vancouverticket.com">
 
 <meta name="
copyright" c ? 2012 Vancouver Ticket">
 <
meta name="designer" c Plantzos Jaryn Bailey Taylor Harris">
 <meta name="
robots" c follow">
 <
meta name="googlebot" c follow">
 <meta http-equiv="
content-language" c>
 
 
 
<style type="
text/css">

.row:before, .row:after {
  display: none;
  content: "";

}
.row { display: block; 
padding-right:-50px;} 



</style> 
</head>
  <div class="
span9" > 
 <div class="
well" > 
<?php echo form_open('contact/emailsender'); ?>
 <h1>Send your message:</h1>
 <table>
  <tr>
   <td><?php echo form_input('sender_name', 'From');?><?php echo form_error('sender_name'); ?></td>    
  </tr>
  <tr>
   <td><?php echo form_input('sender_email', 'Email');?><?php echo form_error('sender_email'); ?></td>
  </tr>
  <tr>
   <td><?php echo form_input('subject', 'Subject');?><?php echo form_error('subject'); ?></td>
  </tr>
  <tr>
   <td><?php echo form_textarea('message', 'Message');?><?php echo form_error('message'); ?></td>
  </tr>
  <tr>
   <td><?php echo form_submit('submit', 'Send');?></td>
  </tr>
 </table>
 <?php echo form_close();?>
 [removed]
  $(function() {
      $('input[type=text], textarea').focus(function() {
       $(this).val('');
    });
   });
 [removed]
</div></div> 
 
Posted: 05 February 2013 03:59 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
4534 posts

When I went to the contact page and clicked submit without filling anything out, It went to /contact/emailSender like your form tells it to submit to and loaded the contact_view with nothing else (no header/footer etc) which is exactly what your code is telling it to do if the validation fails.

From what I can tell, it’s working as coded.  It fails validation so it only loads the form with nothing else like the code tells it to. You aren’t outputting any validation errors on the form so the user would have no idea it failed or why.

 Signature 
 
Posted: 05 February 2013 04:03 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
4534 posts

I’d try 2 things.
In your form, add the validation error output.

echo validation_errors(); 

In your controller for validation, when it fails, try just sending it back to the index method which displays the whole form instead of loading that partial view:

$this->index(); 

instead of

$this->load->view('contact_view'); 

 

 Signature 
 
Posted: 05 February 2013 04:16 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2013-02-05
7 posts

I tried adding the echo in but it still just goes to http://www.vancouverticket.com/contact/emailsender which is a blank page instead of running the function. I’m not getting any errors back or anything.  I tried putting the <?php echo validation_errors(); ?>  line just above the form close and when that didn’t work I tried it in the message box hoping it may echo it back in there.

Why would it be going to contact/emailsender instead of running the process? I’m at a loss.

Where should I put the validation_errors? It didn’t seem to change anything when I tried

EDIT

I realized I missed your original post
If it’s working as it should then I am seriously confused because it seems to be going to the emailsender page or whatever when it should just be running that function and going to success_view as I understood the code to be telling it to do so

 
Posted: 06 February 2013 02:28 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2013-02-05
7 posts

Still stuck on this if anyone can help