首页 > 解决方案 > 如何使用 OCtober CMS 插件组件将表单输入保存到数据库

问题描述

我正在尝试使用十月 CMS 插件组件将用户信息保存到数据库中。

这就是注册页面看起来像注册(register.php)

<h3>Add User</h3>
{% component 'userregistrationform' %}

这是插件的样子

<?php namespace Novaji\Users\Components;

use Input;
use Redirect;
use Validator;
use Novaji\Users\Models\User;
use Db;

class UsersRegistrationForm extends \Cms\Classes\ComponentBase
{
    public function componentDetails()
    {
        return [
            'name' => 'Users Registration Form',
            'description' => 'Accept registration information from users.'
        ];
    }
    /** 
     * This Method Handles User registration
     */
    function onSave(){

        Db::table('users')->insert(
            [
                'token'     => 'Sample Token',
                'email'     => Input::get('email'),
                'firstname' => Input::get('firstname'),
                'lastname'  => Input::get('lastname'),
                'password'  => Input::get('password'),
                'user_type' => 'CUSTOMER',
                'phone'     => post('phone')
            ]
            );
    }
}

插件的组件 default.htm 如下所示:

title = "Register"
url = "/customer/register"
layout = "default"
is_hidden = 0

[userregistrationform]
==
<form data-request="onSave" >
    <input type="text" name="firstname" placeholder="Firstname"/> <br>

    <input type="text" name="lastname" placeholder="Lastname" /> <br>

    <input type="text" name="email" placeholder="Email"/><br>

    <input type="text" name="phone" placeholder="Phone number"><br>

    <button type="submit">Register</button>
</form>

Plugin.php 看起来像这样:

<?php namespace Novaji\Users;

use System\Classes\PluginBase;

class Plugin extends PluginBase
{


    public function registerComponents()
    {
        return [
            'Novaji\Users\Components\UsersRegistrationForm' => 'userregistrationform'
        ];
    }

    public function registerSettings()
    {
    }
}

它没有抛出任何错误,也没有将记录添加到数据库中。请帮助,我已经尝试了一切似乎不知道出了什么问题,甚至没有收到任何错误消息,表单只是弹回

标签: laraveleloquentoctobercmsoctobercms-pluginsoctober-form-controller

解决方案


因此,我能够弄清楚这一点,并决定与将来可能遇到相同问题的任何人分享。
解决方案:我在将自定义 Javascript 添加到themes/author/layout/default.htm. 默认。:

...
...
...
//html scripts before 
<script src="{{ 'assets/javascript/app.js'|theme }}"></script>
{% framework extras %}
{% scripts %}
</body>
</html>

推荐阅读