Welcome Guest, Not a member yet? Register   Sign In
contact form guidance [just started]
#1

[eluser]Met[/eluser]
Hi.

Please find below code for my first attempt at a CI app, a very basic contact form.

It works, but I get the impression I haven't done it "correctly", or there are far better ways to go about it.

Code:
<?php
/* controller */
class Contact extends Controller {

    function Contact ()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->helper(array('form', 'url'));        
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('comments', 'Comments', 'required');
        $this->form_validation->set_rules('phone', 'Phone Number', 'valid_number');
                
        if ($this->form_validation->run() == FALSE)
        {
            //if there are errors, then set $include to the contact-form view
            $include='content/contact-form';
        }
        else
        {
            // no errors...clean up the data
            $name = $this->input->xss_clean($this->input->post('name'));
            $email = $this->input->xss_clean($this->input->post('email'));
            $phone = $this->input->xss_clean($this->input->post('phone'));
            $subject = $this->input->xss_clean($this->input->post('subject'));
            $comments = $this->input->xss_clean($this->input->post('comments'));
                
            // construct the message
            $message=$name . ' says: ';
            $message.=$comments;

            // send the email...
            $this->load->library('email');
            
            $this->email->from('[email protected]', 'contact frm');
            $this->email->to('[email protected]');
    
            $this->email->subject($subject);
            $this->email->message($message);
            $this->email->send();
            
            // set $include to be thanks instead
            $include='content/thanks';
        
        }

            // set data and pass to views.    
            $data['include']= $include;
            $this->load->view('header');
            $this->load->view('body', $data);
            $this->load->view('footer');    
        
    }


}

How else could I go about achieving this? The idea of setting an $include variable seems, somewhat primitive?

Thanks.
#2

[eluser]CroNiX[/eluser]
You can get rid of all of this and put the xss_clean in the rules.
Code:
$name = $this->input->xss_clean($this->input->post('name'));
$email = $this->input->xss_clean($this->input->post('email'));
$phone = $this->input->xss_clean($this->input->post('phone'));
$subject = $this->input->xss_clean($this->input->post('subject'));
$comments = $this->input->xss_clean($this->input->post('comments'));
Just add to your rule definitions, like:
Code:
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
As standard practice, I also use a "trim" rule for all inputs to remove any accidental spaces inputted by user. The rules are executed in order they are listed, so trim should be used before anything else. If not, I believe a user can just enter a single space and that would not trip off the "required" rule, because it already has a value (a space).
Code:
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');

Im not sure what you are doing with your $include, but I would replace that line and just load a new view upon validation failure. Additionally I would add the header before validation, since you will load it upon both pass and fail. Same with the footer, but put under your validation clauses. Something like:
Code:
//load header
$this->load->view('header');

//run validation
if ($this->form_validation->run() == FALSE)
{
    //didn't pass, load the contact form view
    $data = array();  //add whatever data you need to pass to the contact form
    $this->load->view('content/contact-form', $data);
}
else
{
    //passed, load the main form
    $data = array();  //add whatever data you need to pass to the main body
    $this->load->view('body', $data);
}

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

Sorry if some of this doesn't make sense, but I wasn't exactly sure what you were doing with your "include". Hopefully this will help though.
#3

[eluser]Met[/eluser]
thanks that's clarified things considerably.

with regards to the $include,

I have a view called "body" that looks like:

Code:
<div id="content">
&lt;?=$this->load->view($include); ?&gt;
</div>

so by passing

Code:
$data['include']='path/to/my/content');
$this->load->view('body', $data);

Appeared to create a some what "dynamic" template for me. No doubt far better ways to implement this.

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB