Post form OctoberCMS
My form:
<form id="main-contact-form" name="contact-form" ata-request="onSend" data-request-success="alert('Message Sent')"> I can not get the form for publication; where can i place this file? Which file am I editing to send form data fields to my email address? I already set 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; } 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> Use in pages / layouts:
[someForm] == {% component "someForm" %} You get the CMS section in your backend and paste it into the "Code" section of the default.htm layout. I already answered this question on the OctoberCMS.com forum. You can read about it here . Make sure that in any form in which you use this function, there is data-request="onSend" otherwise it will not work. This is how it will eventually look ...

You can add the HTML form either to the component directory of the partial components, or to the partial theme directory, or simply add it directly to any page / layout. It does not really matter.
Learn more about enabling Partials.
{% partial "contact-form.htm" %} or
{% partial __SELF__ ~ "::contact-form.htm" %} // reference to component partial October AJAX framework requires the use of JavaScript APIs or data attributes. This is great, as you do in the example, but forgot to add the component name before the onSend handler
data-request="SendEmails::onSend" Where SendEmails = Component Name or Alias โโindicated on the page, if the form is in the partial part, just use {{ __SELF__ }}::onSend
or with the JavaScript API, just do:
$.request('onSend', { data:{email:email, message:message, name:name}, success: function (data) { // }, error:function(e){ // } }); then in the component handler the request will create the onSend function:
<?php namespace AuthorName\PluginName\Components; use Cms\Classes\ComponentBase; use Mail; use Url; use Input; use Request; use Response; use ApplicationException; use Validator; use ValidationException; class SendEmails extends ComponentBase { public function onSend() { if (Request::ajax()) { try { $data = post(); // Quick Validation rules for E-mail, Name & Message if (!array_key_exists('email', $data)) { $data['email'] = post('email'); } if (!array_key_exists('norad', $data)) { $data['message'] = post('message'); } if (!array_key_exists('name', $data)) { $data['name'] = post('name'); } $rules = [ 'email' => 'required|email|between:6,255', 'name' => 'required|between:4,255' //.. ]; $validation = Validator::make($data, $rules); if ($validation->fails()) { throw new ValidationException($validation); } // Check if E-mail Template Exists @ "author.plugin::mail.templatename" if (View::exists("author.plugin::mail.templatename")) { Mail::send("author.plugin::mail.templatename", $data, function ($message) { $message->from(' noreply@yourdomain.com ', 'Site Name'); $message->to($data['email'], $data['name']); $message->subject('Subject here..'); }); // Handle Erros if (count(Mail::failures()) > 0) { echo "Failed to send Mail "; // Handle Failure } else { // Mail sent echo "Mail Sent!"; // Handle Success } } } catch (Exception $ex) { throw $ex; } } } }