laravel 十月CMS帖子表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29409963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:17:04  来源:igfitidea点击:

OctoberCMS Post Form

phpajaxlaravelsendmailoctobercms

提问by T2T

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;
}

回答by T2T

Please refer to the documentation: Plugin Components.

请参考文档:插件组件

You can create a component (SomeForm.php)

您可以创建一个组件 ( 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)

然后为它创建一个视图(例如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" %}

回答by Saifur Rahman Mohsin

You goto the CMS section in your backend and paste that into the Code section of the default.htm layout. I already answered this question in the OctoberCMS.com forum. You can read about it here. Make sure that whatever form you use this on has a data-request="onSend"else it will not work. This is how it would ultimately look like...

您转到后端的 CMS 部分并将其粘贴到 default.htm 布局的代码部分。我已经在 OctoberCMS.com 论坛中回答了这个问题。你可以在这里阅读它。确保您使用它的任何形式都有data-request="onSend"其他它不起作用。这就是它最终的样子......

enter image description here

在此处输入图片说明

回答by Raja Khoury

You can add the Form's HTML either in your component partials directory, Theme's partial directory or just add it directly to any page / layout. It does not really matter.

您可以在组件部分目录、主题的部分目录中添加表单的 HTML,也可以直接将其添加到任何页面/布局。这真的不要紧。

Read more about including Partials

阅读更多关于包括部分

{% partial "contact-form.htm" %} 

Or

或者

{% partial __SELF__ ~ "::contact-form.htm" %} // reference to component's partial

October's AJAX framework requires the use of the JavaScript API or data attributes. It is fine how you are doing it in the example but forgot to add the Component's Name before the onSend Handler

十月的 AJAX 框架需要使用 JavaScript API 或数据属性。您在示例中的操作方式很好,但忘记在 onSend 处理程序之前添加组件的名称

data-request="SendEmails::onSend" 

Where SendEmails= Component Name or Alias given in the page, if the form is in the component's partial just use {{ __SELF__ }}::onSend

where SendEmails= 组件名称或页面中给出的别名,如果表单在组件的部分中,请使用{{ __SELF__ }}::onSend

or with the JavaScript API, just do :

或者使用 JavaScript API,只需执行以下操作:

$.request('onSend', {
    data:{email:email, message:message, name:name},
    success: function (data) {
      //
     },
    error:function(e){
      //
    }
 });

then in the component handling the request create a function onSend:

然后在处理请求的组件中创建一个函数onSend

<?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('[email protected]', '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;
            }
        }
    }

}