Contact form tutorial

edited March 2018 in News & Announcements

Is there a contact form tutorial for fuel?


  • edited 1:06AM
    Have you checked out the Forms module:
  • edited 1:06AM
    Hi admin, it says it is for version 0.93, so nothing changed?
  • edited 1:06AM
    Hi again, looking at the link you sent me, I still don't understand how to create the form.

    "Create forms in the CMS, as static views or a combination of the two" where in the CMS?

    "Below is an example of adding a hook in the configuration file of a form:" what is a configuration file of a form? Where is it? Do I need to create it? if so, where do I save it?

    "Using the 'form' helper function
    // load the helper ... $CI is CI object obtained from the get_instance(); and automatically passed to blocks
    $CI->load->module_helper(FORMS_FOLDER, 'forms');

    echo form('myform', array('fields' => array('name' => array('required' => TRUE)));"

    Where do I use it? In a controller? In a page in the cms? in a view?

    "To add the form to a page in the CMS, you will need to add the "form" function to the $config['parser_allowed_functions'] configuration in the fuel/application/config/MY_fuel.php file"
    Add it how?

    So as you can see, I don't understand the process at all.

    Any way you can help please?
  • edited 1:06AM
    Ok, I created a block with the form (I want to display the form on the home page)

    The form's action is: mysite/contact/process

    I have a contact controller and in it I have a function: process.

    In the Email.php file I have this:
    public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
    public $protocol = 'mail'; // mail/sendmail/smtp

    In MY_Config I have this (for local testing):
    $config['dev_email'] = Array(
    'protocol' => 'smtp',
    'smtp_host' => '',
    'smtp_port' => 2525,
    'smtp_user' => 'my-user',
    'smtp_pass' => 'my-password',
    'authentication' => 'cram_md5',
    'crlf' => "\r\n",
    'newline' => "\r\n"

    On the live server I get Page not Found.

    On the local server I get:
    Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

    Can you please help?
  • edited 1:06AM
    For the email settings, they should go in fuel/applications/config/email.php (which you may need to create) and not MY_Config.php.

    Just to confirm, you downloaded the module from the following URL and installed it per the instructions on that page?

    The easiest way to test out the form is to copy the fuel/modules/forms/config/forms.php file into fuel/application/config/forms.php. Then go to the forms area of the config and add your configuration. Then in create a file in fuel/application/views/test.php added the following:
    Where 'my_form' is the key given to the form in the config.
  • edited 1:06AM
    Thank you, but still can't make it work.

    I changed my approach and downloaded the demo site from the user guide.

    I put these details in the Email file:
    var $mailpath = "";
    var $protocol = "smtp";
    var $smtp_host = "ssl://";
    var $smtp_user = "myuser";
    var $smtp_pass = "mypass";
    var $smtp_port = "465";
    var $smtp_timeout = 5;
    var $smtp_crypto = "";
    var $wordwrap = TRUE;
    var $wrapchars = "76";
    var $mailtype = "html";
    var $charset = "iso-8859-1";

    Tried to send the contact form from my local pc, got this:
    Message: fsockopen(): SSL: Handshake timed out

    Filename: libraries/Email.php

    Line Number: 1690

    In my php.ini I have this line:

    I also tried to follow what was done in this site, and found out I need to create these tables: forms and form_entries which I am not interested in, don't need to save any form entries.

    Also, what is the $config['dev_email'] for in the MY_Config file?

    Thank you very much for all the help.
  • edited 1:06AM
    Oh, another question, how can I set different email servers for different environment if I don't do it in the MY_Config file?
  • edited 1:06AM
    Sorry, another question, which version of ci is the demo site using?

    I uploaded the site to my live server (php version 7) and I get this error:
    Message: Declaration of MX_Loader::library($library, $params = NULL, $object_name = NULL) should be compatible with CI_Loader::library($library = '', $params = NULL, $object_name = NULL)

    Filename: MX/Loader.php

    Line Number: 0
  • edited March 2018
    Hi Sigal, I recently began putting Fuel CMS sites onto PHP 7 servers, and it would be easier to use Fuel v1.4 (which includes CI v3.1.3). You need to ensure that libraries, models and controllers have filenames that match their class names exactly eg controllers/Contact.php (not controllers/contact.php). Otherwise copying the newest version's fuel/modules/fuel/ folder over the old is a good start, but obviously, files in /application will need to be changed suitably too.

    There's a command line utility that might be of use mentioned in the above link.

    You might be able to downgrade your PHP on the server also (to v5.5/6). But best to get used to PHP7!
  • edited March 2018
    Re environment settings & email you could do something like:
    $email_cfgs = [ 'development' => [ 'protocol' = 'sendmail', 'mailpath' = '/usr/sbin/sendmail', 'charset' = 'iso-8859-1', 'wordwrap' = TRUE, ], 'production' => [ ... different settings... ] ]; $this->email->initialize($email_cfgs[ENVIRONMENT]);

    I'm not sure where you want to place the initialization (in a controller, library, config) so you might need to adjust, eg for a config/email.php you would only need:

    $dev = [ 'protocol' => 'sendmail', 'mailpath' => '/usr/sbin/sendmail' ...etc.. ]; $prod = [ 'protocol' => 'smtp', 'smtp_port' => 25 ...etc.. ]; $config = (ENVIRONMENT == 'production') ? $prod : $dev;
  • edited 1:06AM
    Thank you Gumster. The problem is I can't understand how to do the contact form, so I downloaded the demo site and it seems it is on php 5.6. The new site I'm trying to develop is on php7
  • edited 1:06AM
    Ok, I managed to send the form. I do not use the form module or builder, I just use a normal html form.
  • edited 1:06AM
    A different problem I have now is with this:

    I get this error:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: __antispam___field

    Filename: _blocks/contact_form.php

    I have the forms file under config:
    $config['forms']['forms'] = array(
    'contact' => array('javascript_validate' => TRUE, 'javascript_submit' => FALSE, 'form_display' => 'block', 'block_view' => 'contact_form', 'form_builder' => array('required_text' => ''),
    'fields' => array(
    'first_name' => array('required' => TRUE),
    'surname' => array('required' => TRUE),
    'email' => array('required' => TRUE),
    'message' => array('type' => 'textarea'),

    I just can't get it to work, and I can't follow the documentation. I don't understand it. Gumster, would you be interested to code the form for me for payment?
  • edited 1:06AM
    I tried all kinds of things, nothing works:

    In my forms.php file I have this:
    $config['forms']['forms'] = array(
    'contact' => array('javascript_validate' => TRUE, 'javascript_submit' => FALSE, 'form_display' => 'block', 'block_view' => 'contact_form', 'form_builder' => array('required_text' => ''),
    'fields' => array(
    'first_name' => array('required' => TRUE),
    'surname' => array('required' => TRUE),
    'email' => array('required' => TRUE),
    'message' => array('type' => 'textarea'),
    'anti_spam_method' => array('method' => 'recaptcha', 'recaptcha_site_key' => 'my-site-key',
    'recaptcha_secret_key' => 'my-secret-key', 'theme' => 'white'),

    In views/_blocks/contact_form I tried:
    1. <?php
    $this->fuel->forms->get('contact'); ?>
    No form displays.

    $CI->load->module_helper(FORMS_FOLDER, 'forms');

    echo form('contact', array('fields' => array('name' => array('required' => TRUE))));

    I get:
    Severity: Notice

    Message: Trying to get property of non-object

    Filename: libraries/Fuel_forms.php

    Line Number: 1216

    I really don't understand, how do I get the form from the forms.php into my view block?

    3. foreach($fields as $field) : ?>



    <?php endforeach; ?>

    Severity: Notice

    Message: Undefined variable: fields

    Filename: _blocks/contact_form.php

    Line Number: 2

    I really need help with this, I'm approaching the site's deadline and just cannot understand the documentation.
  • edited 1:06AM

    If you don't need or care about the user / admin being able to define custom forms, then be aware there are other ways of providing contact forms. Generally I would create my own controller, use Form_builder class to create a form from a config file and set the rendered HTML as a variable sent to the controller's view. This is very much inline with regular Codeigniter principles. The controller would include the validation rules (if any, for the form) and the validated POST can then be emailed, post processed or whatever. But you lose (somewhat) the flexibility of the Forms module, having a block that can be rendered anywhere, etc.

    I'm using the Forms module exactly for those admin functionality reasons, and it seems to work fine, but I am using v1.4, PHP7 and storing the form config in the Forms module db table. There was a hiccup I encountered early on, but that has been fixed now.

    I'd be interested in helping out, you can use this forum to send a message via my profile page if you are still stuck.
  • edited 1:06AM
    To get the form into the block, you should just need the form helper - it calls on Form_builder class to render the form as HTML (and populate with any posted values too).
Sign In or Register to comment.