php - HTML 表单中的多个 PHP 函数处理程序?

标签 php html forms octobercms

我正在使用 OctoberCMS基于 LaravelTwig .

我正在尝试使用 1 个表单和 2 个提交按钮从 2 个 php 文件调用 2 个不同的函数。

form

我连接我制作的组件,Edit.phpDelete.php , 到一个页面。

我没有将 Form 操作指向/Edit.php,而是将其指向当前 url 并使用处理程序调用 php 函数 onEdit()Edit.php ,使用编辑提交按钮。

同样的形式,如何调用onDelete()Delete.php使用删除提交按钮?

它只选择 1 个处理程序并调用其函数。

我无法将这两个函数合并到 1 个 php 文件中。

https://jsfiddle.net/t3t5e2Ln/

表格

<!-- Edit or Delete -->
<form method="POST" action="{{ url_current() }}" enctype="multipart/form-data">

    <!-- Edit Handler -->
    <input type="hidden" name="_handler" value="onEdit" />

    <!-- Delete Handler -->
    <input type="hidden" name="_handler2" value="onDelete" /> 

    <!-- Title -->
    <input type="text" name="title" maxlength="255" />

    <!-- Edit Submit -->
    <input type="submit" name="edit" value="Edit" />

    <!-- Delete Submit -->
    <input type="submit" name="delete" value="Delete" />

</form>

编辑.php

public function onEdit() {
    //edit
}

删除.php

public function onDelete() {
    //delete
}

最佳答案

之前的两个答案都不正确。参见 http://octobercms.com/docs/cms/componentshttp://octobercms.com/docs/plugin/components有关为 10 月开发自定义组件的更多信息。

首先,我会将两个处理程序放在同一个组件中(RecordData.php 作为名称的示例)。其次,您应该利用 October 出色的 AJAX 框架:http://octobercms.com/docs/ajax/introduction .

这是您的 RecordData.php 组件类的示例:

<?php namespace MyVendor\MyPlugin\Components;

use Auth;
use Flash;
use Cms\Classes\ComponentBase;

use MyVendor\MyPlugin\Models\MyRecord as MyRecordModel;

class RecordData extends ComponentBase
{    
    /**
     * Provide information about the component
     */
    public function componentDetails()
    {
        return [
            'name'        => 'RecordData Component',
            'description' => 'Used for editing and deleting custom record data'
        ];
    }

    /**
     * Register any component properties
     * http://octobercms.com/docs/plugin/components#component-properties
     */
    public function defineProperties()
    {
        // return [];
    }

    /**
     * Get the record that will be edited, factoring in the user's access to the specified record
     *
     * @return MyRecordModel
     */
    public function getRecord()
    {    
        $user = Auth::getUser();

        if ($user) {
            return MyRecordModel::findOrFail(post('recordId'));
        } else {
            throw new \Exception("You do not have access to do that.");
        }
    }

    /**
     * AJAX Handler for editing data
     */
    public function onEdit()
    {
        // Get the record
        $record = $this->getRecord();

        // Modify the record
        $record->title = post('title');

        // Save the modifications to the record
        $record->save();

        // Notify the user that the record has been edited
        Flash::success("Record successfully edited");
    }

    /**
     * AJAX Handler for deleting data
     */
    public function onDelete()
    {
        // Get the record
        $record = $this->getRecord();

        // Delete the record
        $record->delete();

        // Notify the user that the record has been deleted
        Flash::success("Record deleted");
    }
}

然后是你的 default.htm部分呈现该组件将如下所示:

{{ form_open() }}

    {# Hidden form field containing the record id. Passing the record id through the URL or other means is possible as well #}
    <input type="hidden" name="recordId" value="{{ __SELF__.recordId }}">

    {# Title #}
    <input type="text" name="title" maxlength="255" />

    {# Edit Button #}
    <button type="button" data-request="{{ __SELF__ }}::onEdit">Save changes</button>

    {# Delete Button #}
    <button type="button" data-request="{{ __SELF__ }}::onDelete">Delete record</button>

{{ form_close() }}

让我向您介绍上面粘贴的 Twig 标记:

{{ form_open() }} and {{ form_close() }} 是在 10 月份注册的 Twig 辅助函数,用于简化创建表单元素的操作,因为它们会自动确保生成正确的标记,并且如果您选择在您的网站上启用它,它会包含 csrf token 隐藏字段。

{# #}表示 Twig 中的注释 block ,我通常更喜欢使用它们而不是 <!-- -->因为这样评论只会对查看 Twig 文件实际源代码的人可见,而不是您网站的所有用户。

data-request利用 attributes API October AJAX 框架指示 AJAX 框架服务器上的哪个方法将负责处理由 data-request 元素触发的请求属性包含在。

{{ __SELF__ }}指的是当前组件,它实际上会降低该组件别名的值。基本上,您需要知道的是,使用它将使您能够在将来一次在页面上放置多个组件。 ::onDelete::onEdit{{ __SELF__ }}之后是什么告诉服务器您想在 {{ __SELF__ }} 指定的组件上运行什么方法?处理该 AJAX 请求。

关于php - HTML 表单中的多个 PHP 函数处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42639270/

相关文章:

html - 将字体导入 css 或 html?

HTML/CSS3 表格行 : first 2 rows white, 接下来的 2 行是灰色的,然后接下来的 2 行又是白色的,依此类推?

html - Adsense 广告导致边栏在 flex 布局中离开屏幕

html - 单击文本以选择相应的单选按钮

php - 确定美国本地/国际电话号码

php - 如何在我的 pdo 代码中添加条件

php - 在提交前设置 PHP 值

javascript - javascript表单验证中的可访问性

php - Android PHP PDO MySQL UPDATE 语法错误

php - 如何找出这个数据库插入和检索在哪里中断?