0

I am trying to work with https://github.com/jackilyn/bootstrap-contact/ to add a bootstrap contact form to my codeigniter project.

The form is submitted as follows:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">

after submission and validation, the following code is used to email the contact info:

//If there is no error, send the email
    if(!isset($hasError)) {
    $emailTo = 'you@yourwebsite.com'; // Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nPhone Number: $phone \n\nSubject: $subject \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
}

I don't like the way the php code is included in the page itself and would like to move it to a seperate controller for security. How can I manage the program flow so that after the form is submitted with no errors , the flow is directed to a controller so I can execute the emailing code there instead , while keeping the form pointing to itself?

Thanks in advance,

Bill

Sherbrow
  • 17,279
  • 3
  • 64
  • 77
user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

3

hi please check i have created contact us controller and view for you really simple

controller http://scrp.at/bYk

class contact extends CI_Controller{

    function index(){

        $this->validation->rules('name','Name', 'required|trim');
        $this->validation->rules('email','Email', 'required|trim|vaild_email');
        $this->validation->rules('message','Message', 'required|trim');

        if(!$this->validation->run()){
            $this->load->view('form_view');
        }
        else{
            $this->send();  
        }

    }

    function send(){
        $this->load->library('email');

        $input = $this->input->post();

        // will extract all variables from post array
        extract($input);

        $this->email->from($email, $name);
        $this->email->to('admin@example.com');
        //$this->email->cc('another@another-example.com');
        //$this->email->bcc('them@their-example.com');

        $this->email->subject('Email Test');
        $this->email->message($message);

        $this->email->send();

        //echo $this->email->print_debugger();

        redirect('redirect-to-thank-you');
    }

}

view http://scrp.at/bYl

<?
echo form_open(uri_string());
echo form_label('Name','name').form_input('name',set_value('name')).form_error('name').br();
echo form_label('Email','email').form_input('email',set_value('email')).form_error('email').br();
echo form_label('Message','message').form_textarea('message',set_value('message')).form_error('message').br();
echo form_close();
?>
umefarooq
  • 4,540
  • 1
  • 29
  • 38
  • You really need to include the code here. Using outside sources to hold the code means this question and answer become useless for people searching for this answer in the future if the outside source is lost. – Rick Calder Dec 04 '12 at 22:51
1

Did you know that a <form> with an empty action="" will submit the form to the same page ? So if it's the same page, you can simply let the attribute empty.

Edit : Leaving the action empty is not allowed by the spec (as seen here), but you can use uri_string() for the current url.


You could also type an URL that you know will redirect to the same page based on your routing (doc).


If you feel good about routing, you can try this reverse routing class that will help you use the routes in your view.

Community
  • 1
  • 1
Sherbrow
  • 17,279
  • 3
  • 64
  • 77