OctoberCMS Post Form

T2T picture T2T · Apr 2, 2015 · Viewed 12.5k times · Source

My form:

<form id="main-contact-form" name="contact-form" ata-request="onSend" data-request-success="alert('Message Sent')">

I cant seem to get a form to post; where do I place this file? Which file do I edit to make it send the form data fields to my email? I have already setup the backend mail settings:

function onSend()
{
    // Collect input
    $name = post('name');
    $email = post('email');
    $message = post('message');


    // Submit form
    $to = System\Models\MailSettings::get('sender_email');
    $params = compact('name','email');
    Mail::sendTo($to, 'temp.website::mail.newrequest', $params);
    return true;
}

Answer

user684202 picture user684202 · Apr 2, 2015

Please refer to the documentation: Plugin Components.

You can create a component (SomeForm.php)

<?php namespace My\Plugin\Components;

use Cms\Classes\ComponentBase;

class SomeForm extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name'        => 'Form',
            'description' => 'Some form'
        ];
    }

    public function onSend()
    {
        // Collect input
        $name = post('name');
        $email = post('email');
        $message = post('message');

        // Submit form
        $to = System\Models\MailSettings::get('sender_email');
        $params = compact('name','email');
        Mail::sendTo($to, 'temp.website::mail.newrequest', $params);
        return true;
    }
}

And then create a view for it (e.g. default.htm)

<form id="main-contact-form" name="contact-form" data-request="{{ __SELF__ }}::onSend" data-request-success="alert('Message Sent')">
    ...
</form>

Usage in pages/layouts:

[someForm]
==
{% component "someForm" %}