Quick Tutorial Needed - Contact Form

edited December 2010 in Feature Requests
Hello,
If possible, I'd like someone to post a quick tutorial (including where to place files) on how to create a simple "Contact" form that sends an e-mail with the contents of the form.
Specifically, I was curious if it was possible to still allow editing of the form-page contents from within the editor while still retaining the functionality of the form.
Any pointers/tips will be greatly appreciated!

Comments

  • edited 9:40AM
    The 0.91 branch has an example website that uses a contact controller you can use as an example.
    https://github.com/daylightstudio/FUEL-CMS/tree/0.91

    However, the page is being pulled from the contact view file instead of the CMS. To have it pulled from the CMS, open up the contact controller and change line 40 to:
    $page_init = array('location' => 'contact', 'render_mode' => 'cms');

    Hope that helps.
  • edited December 2010
    Awesome!
    This was just what I was looking for, however I'm running into an error when trying to load the Contact page:

    Fatal error: Class 'CI_Controller' not found in /home/fuel/application/controllers/Contact.php on line 2

    Thanks in advance for the assistance! FUEL rocks!

    Any suggestions?
  • edited December 2010
    Fixed it!

    Changed line 2 to the following:
    class Contact extends Controller {

    (changed CI_Controller to Controller)

    And now the form displays, but it doesn't seem to be accepting the post data correctly (perhaps session->flashdata isn't working correctly). Fill in the form and hit submit, yet it just takes me back to the form itself, no message, mail sent, etc.

    Also - Here's what I have in my View (Input at Fuel Admin -> Pages):
    <h1>Contact Us</h1> {if ($session->flashdata('success')) } <p class="success">Thank you for contacting us. We will get back to you shortly.</p> {else } <p>Please fill out the form below to send us any questions:</p> {$form} {/if}
  • edited 9:40AM
    Aha! I answered my own question again... I logged out of the Admin and now it works. Must have been something interfering with the submission of the form when I was logged in as admin.

    This is awesome! Thank you thank you!
  • edited 9:40AM
    No worries. As an FYI, the CI_Controller is CI 2.0 change and that's why you were seeing that error. I'd be curious to know more about what the problem was that you were having. The example provided in the 0.91 version does require you to set the configuration value of "dev_email" in the MY_config.php and may have contributed to the problem you were seeing but am not certain.
  • edited 9:40AM
    Yes, I did set the config value of "dev_email" in MY_config.php, so I don't think it has to do with that.
    Thanks for the heads-up on the CI_Controller vs Controller in CI 2.0. Guess I'd better read something about the changes seeing as it's here already!

    In any case, it's working like a charm now. One item that you may want to add to the User_Docs for the Form Builder Class @ http://www.getfuelcms.com/user_guide/libraries/form_builder (I had to look it up in the CI libraries documentation).

    A quick example of how to configure the library such as:
    $form_params = array('id'=>'contact_form','submit_value' => 'Send Message', 'textarea_rows' => '5', 'textarea_cols' => '28');
    $this->load->library('form_builder',$form_params);

    In any case, thanks for the great work. I'm just working on getting my first project completed with FUEL... it's great, and much better suited to what I need to accomplish than a more-bloated CMS like EE.
  • edited December 2010
    I've added that for the Form_builder class and the other libraries in the documentation.
  • edited February 2011
    Hello,

    I'm trying to implement contact form on my clint website, I took all code form your demo website. But it doesn't work, on submission it just reload the contact page.
    I'm running the latest build of FUEL CMS, and here is what i have in contact controller:

    class Contact extends CI_Controller {

    function __construct()
    {
    parent::__construct();
    }

    function index()
    {
    $this->load->library('session');
    $this->load->library('form_builder');


    if (!empty($_POST) AND $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR'])
    {
    // put your processing code here... we show what we do for emailing. You will need to add a correct email address
    if ($this->_process($_POST))
    {
    $this->session->set_flashdata('success', TRUE);
    redirect('contact');
    }
    }

    $fields = array();
    $fields['first_name'] = array('label' => 'Имя','required' => TRUE);
    $fields['last_name'] = array('label' => 'Фамилия','required' => TRUE);
    $fields['email'] = array('required' => TRUE);
    $fields['question'] = array('label' => 'Ваш Вопрос','required' => TRUE, 'type' => 'textarea');

    $this->form_builder->set_fields($fields);

    // will set the values of the fields if there is an error... must be after set_fields
    $this->form_builder->set_validator($this->validator);
    $this->form_builder->set_field_values($_POST);
    $this->form_builder->display_errors = TRUE;
    $vars['form'] = $this->form_builder->render();

    // use Fuel_page to render so it will grab all opt-in variables and do any necessary parsing
    $page_init = array('location' => 'contact', 'render_mode' => 'cms');
    $this->load->module_library(FUEL_FOLDER, 'fuel_page', $page_init);
    $this->fuel_page->add_variables($vars);
    $this->fuel_page->render();
    }

    function _process($data)
    {
    $this->load->library('validator');
    /*
    Set rules up here so we can pass them to the form_builder to display errors.
    validator_helper contains the valid_email function... validator helper automatically gets' looded with Validation Class'
    */


    $this->validator->add_rule('first_name', 'required', 'Please enter in an first name', $this->input->post('first_name'));
    $this->validator->add_rule('last_name', 'required', 'Please enter in an last name', $this->input->post('last_name'));
    $this->validator->add_rule('email', 'valid_email', 'Please enter in a valid email', $this->input->post('email'));
    $this->validator->add_rule('question', 'required', 'Please enter in an question', $this->input->post('question'));


    if ($this->validator->validate())
    {
    $this->load->library('email');
    $this->load->helper('inflector');

    // send email
    $this->email->from($data['email'], $data['first_name'] . ' ' . $data['last_name']);

    /*********************************************************************
    YOU MUST FILL OUT THE CORRECT dev_email config in application/config/MY_config.php
    AND/OR THE CORRECT TO email address
    *********************************************************************/
    // check config if we are in dev mode
    if ($this->config->item('dev_mode'))
    {
    $this->email->to($this->config->item('dev_email'));
    }
    else
    {
    // need to fill this out to work
    $this->email->to('sales@buyautoinusa.com');
    }
    $this->email->subject('Website Contact Form');
    $msg = "The following information was submitted:\n";
    foreach($data as $key => $val)
    {
    $msg .= humanize($key, 3).": ".$val."\n";
    }
    $this->email->message($msg);

    // let her rip
    if (!$this->email->send())
    {
    add_error('There was an error notifying');
    return FALSE;
    }

    return TRUE;
    }

    }
    }


    and my view:

    <?php fuel_set_var('layout','with_sidebar') ?>

    Контакты

    <?php if ($this->session->flashdata('success')) : ?>

    Спасибо! Мы скоро с вами свяжемся!

    <?php else : ?>

    Пожалуйста, заполните контактную форму:

    <?php echo $form; ?>
    <?php endif; ?>


    please let me know what I'm doing wrong?

    I'd really appreciate your help!
  • edited 9:40AM
    Try removing $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']
  • edited 9:40AM
    thanks that worked
  • edited 9:40AM
    Hello !

    I have the contact form working properly, but in the dashboard I have a new menu called "Modules" with "Contact" under it, and when I click on it, it say "An Error Was Encountered
    Unable to locate the file: contact_model.php"
    Do you know what this mean ?

    Thanks you very much,
  • edited 9:40AM
    That sounds like you create a simple module using the fuel/applicaiton/config/MY_fuel_modules.php? If so, there needs to be a contact_model for the module to map to. By default it assumes that the model is the name of the module with the "_model" as a suffix.
  • edited 9:40AM
    githubs link show not found..
  • edited 9:40AM
    That branch has probably been deleted by now, as FUEL CMS is now in version 1.0.5 .

    However, I have been developing an advanced module with Contact Form functionality.
    https://github.com/kbjohnson90/FUEL-CMS-Contact-Module
Sign In or Register to comment.