Welcome Guest, Not a member yet? Register   Sign In
Standard contact form
#1

[eluser]-sek[/eluser]
Is there standard CI code available for implementing a contact form? I've searched the forums, but could not really find a standard module for this. Thanks.
#2

[eluser]llbbl[/eluser]
its basic form processing. they have all the libraries you need included with CI to allow you to build your own contact form rather quickly.
#3

[eluser]Référencement Google[/eluser]
I post you one that I use. There is maybe a better code to do here for sure, but at least it is working. I post it "as is" but you should make some adaption of the code because I am using a language file. If you come to a more nicer code, maybe should you post it on the wiki.

Controller:
Code:
<?php

require_once('main.php');

class Contact extends Main {

    function Contact()
    {
        parent::Main();
        $this->load->language('contactform');
    }
                
    function index()
    {
        $data['content'] = '';
        
        /** ----------------------------------------------------
         *   Start contact form and validation
         ** ---------------------------------------------------- */
        
        // Setup the html delimiter for errors
        $this->validation->set_error_delimiters('<p class="form_error">', '</p>');
        
        // Setup rules for the validation of the contact form
        $rules = array(    'prenom'         => 'trim|required|max_length[50]',
                        'nom'             => 'trim|max_length[50]',
                        'telephone'     => 'trim|max_length[25]',
                        'contactemail'    => 'trim|required|valid_email|max_length[120]',
                        'message'         => 'required');

        $this->validation->set_rules($rules);
        
        // Setup fiels to repopulate
        $fields = array('prenom'         => $this->lang->line('firstname'),
                        'nom'             => $this->lang->line('surname'),
                        'telephone'     => $this->lang->line('phone'),
                        'contactemail'  => $this->lang->line('email'),
                        'message'         => $this->lang->line('message'));
                        
        $this->validation->set_fields($fields);
        
        if($this->validation->run())
        {
            // All ok, we send the email
            if($this->_send_email())
            {
               redirect('contact/success');
            }
            else
            {
                $data['result_message'] = '<p class="form_error">Votre message n\'a pas pu être envoyé !<br >
                Veuillez réessayer ou utiliser nos autres moyens de contacts.</p>';
                $data['content'] = $this->load->view('contact_form', $data, true);
            }
        }
        else
        {
            $data['content'] = $this->load->view('contact_form', '', true);
        }
      
        $this->load->view('contact', $data);
    }
    
    function _send_email()
    {        
        $this->email->from($this->validation->value('contactemail'), $this->validation->value('nom').' '.$this->validation->value('prenom'));
        $this->email->to($GLOBALS['site_email']);
        
        $this->email->subject('Formulaire contact NIPX');        
        
        $data['toName']     = '';
        $data['mailIntro']  = 'Un message vous a été envoyé depuis la page contact du site NIPX.';
        
        // Prepare the message body        
        $data['message']    = "---------------- COORDONNEES ----------------\n";        
        $data['message']   .= "Nom: ".$this->validation->value('nom')."\n";
        $data['message']   .= "Prénom: ".$this->validation->value('prenom')."\n";
        $data['message']   .= "Téléphone: ".$this->validation->value('telephone')."\n";
        $data['message']   .= "Email: {unwrap}".$this->validation->value('contactemail')."{/unwrap}\n\n";
        
        $data['message']   .= "---------------- MESSAGE ----------------\n";
        $data['message']   .= $this->validation->value('message')."\n";
        
        $message = $this->load->view('emails/email_txt', $data, true);        
        $this->email->message($message);
          
        if($this->email->send())
        {          
            return true;
        }
    }
    
    function success()
    {
        $data['content'] = '<p class="form_ok">Votre message a bien été envoyé !<br />
        Une réponse vous parviendra prochainement.</p>';
        $this->load->view('contact', $data);
    }
}

?&gt;
#4

[eluser]Référencement Google[/eluser]
The view:
Code:
&lt;form id="contact_form" action="&lt;?=site_url($this-&gt;uri->uri_string().'#contact_form')?&gt;" method="post">
    
&lt;?php
    if(isset($result_message))
    {
        echo $result_message;
    }
?&gt;

<fieldset>
    
    <h3>Formulaire de contact</h3>
        
    <div class="form_element">
        <label for="nom">&lt;?=$this->lang->line('surname')?&gt;</label>
        &lt;input name="nom" id="nom" value="&lt;?=$this-&gt;validation->value('nom')?&gt;" size="29" maxlength="50" type="text" />
        <a class="info" href="#">?<span>&lt;?=$this->lang->line('help_surname')?&gt;</span></a>
    </div>
    
    <div class="form_element">
        &lt;?=$this->validation->error('prenom')?&gt;
        <label for="prenom"><span class="requiered">*</span> &lt;?=$this->lang->line('firstname')?&gt;</label>
        &lt;input name="prenom" id="prenom" value="&lt;?=$this-&gt;validation->value('prenom')?&gt;" size="29" maxlength="50" type="text" />
        <a class="info" href="#">?<span>&lt;?=$this->lang->line('help_firstname')?&gt;</span></a>
    </div>
        
    <div class="form_element">
        <label for="telephone">&lt;?=$this->lang->line('phone')?&gt;</label>
        &lt;input name="telephone" id="telephone" value="&lt;?=$this-&gt;validation->value('telephone')?&gt;" size="29" maxlength="25" type="text" />
        <a class="info" href="#">?<span>&lt;?=$this->lang->line('help_phone')?&gt;</span></a>
    </div>
    
    <div class="form_element">
        &lt;?=$this->validation->error('contactemail')?&gt;
        <label for="contactemail"><span class="requiered">*</span> &lt;?=$this->lang->line('email')?&gt;</label>
        &lt;input name="contactemail" id="contactemail" value="&lt;?=$this-&gt;validation->value('contactemail')?&gt;" size="29" maxlength="120" type="text" />
        <a class="info" href="#">?<span>&lt;?=$this->lang->line('help_email')?&gt;</span></a>
    </div>
        
    <div class="form_element">
        &lt;?=$this->validation->error('message')?&gt;
        <label for="message"><span class="requiered">*</span> &lt;?=$this->lang->line('message')?&gt;</label>
        &lt;textarea name="message" id="message" rows="7" cols="45"&gt;&lt;?=$this->validation->value('message')?&gt;&lt;/textarea&gt;
        <a class="info" href="#">?<span>&lt;?=$this->lang->line('help_message')?&gt;</span></a>
    </div>
    
    <div class="form_element">
      <label>&nbsp;</label>
      <button type="submit">&lt;?=$this->lang->line('submit')?&gt;</button>
    </div>
    
    <div class="form_element">
        <div class="label">&nbsp;</div>
        <p><span class="red">*</span> Champs obligatoires</p>
    </div>

</fieldset>

&lt;/form&gt;

Also, look at the validation in the view file because I am using a custom library for validation, you will have to make some adaption according to CI user guide to make it work correctly.

Hope this help
#5

[eluser]llbbl[/eluser]
A good thing to do with contact forms is to have hidden fields with css to catch bots, otherwise you will get spamed like crazy.




Theme © iAndrew 2016 - Forum software by © MyBB