【问题标题】:Multiple PHP Function handlers in HTML Form?HTML 表单中的多个 PHP 函数处理程序?
【发布时间】:2017-07-27 03:04:45
【问题描述】:

我正在使用基于LaravelTwigOctoberCMS

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

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

我没有将表单操作定向到 /Edit.php,而是将其定向到当前 url,并使用处理程序通过编辑提交按钮调用 Edit.php 中的 php 函数 onEdit()

在同一个表单中,如何使用删除提交按钮在Delete.php 中调用onDelete()

它只选择 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
}

【问题讨论】:

  • 只用两种形式?对于非常简单的事情,这是一个不必要的复杂解决方案。两种形式,如果需要,适当地设计它们以保持您想要的外观。
  • @junkfoodjunkie 我现在正在使用两种表单来解决这个问题,但是我的布局设置方式,我无法使用 css 将两个提交按钮放置在彼此旁边.
  • 为什么?无论布局如何,绝对没有什么可以阻止您根据需要重新排列这些提交按钮。但是,是的,无论如何,在同一个表单中拥有两个提交按钮绝不是明智之举,除非您采取预防措施来防止用户在表单中按下“Enter”。

标签: php html forms octobercms


【解决方案1】:

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

首先,我会将两个处理程序放在同一个组件中(RecordData.php 作为名称的示例)。其次,您应该利用 10 月份出色的 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 令牌隐藏字段.

{# #} 代表 Twig 中的评论块,我通常更喜欢使用它们而不是 &lt;!-- --&gt;,因为这样评论只会对查看 Twig 文件的实际源代码的人可见,而不是您网站的所有用户.

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

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

【讨论】:

  • 非常好的信息。请允许我花一些时间来解决这个问题并回复您。
  • 我在我的页面上包含了 Twig 表单标记我收到错误:未知的“form_open”标签。我应该在“代码”部分中包含哪些帮助程序以及将其放在哪里?
  • 我认为您的标记中有错字,它是 {{ 而不是我在文档中注意到的 {%。
  • 很好,谢谢!在编写伪代码时,我有时很容易混淆它们:)
  • 在执行 2 个功能之前,我正在尝试在较小的表单上对其进行测试。在此表单上,您在文本框中输入记录名称,然后按删除。但是当数据请求添加到输入按钮时,表单不会提交,它只是停留在同一页面上。它无需使用树枝即可工作。这是代码kopy.io/HL0jm
【解决方案2】:

通常这应该通过ajax请求来完成,所以你可以独立选择端点,但使用几个js也可以做到这一点。

$('input[type=submit]').on('click', function(e) {
   e.preventDefault();
   var url = '/Edit.php';
   if (this.name == 'delete') {
        url = '/Delete.php';
   }
   this.form.action = url;
   this.form.submit();
});

【讨论】:

    【解决方案3】:

    在你的标题中

    <head>
    <script language="javascript" src="editordelete.js" type="text/javascript"></script>
    </head>
    

    你的按钮

    <input type="button" name="edit" value=edit  onClick="seteditAction();" />
    <input type="button" name="edit" value=edit  onClick="setdeleteAction();" />
    

    创建一个名为“editordelete.js”的文件并将其放入其中,将您的表单名称放在我写yourformnamesgoeshere的位置

    function seteditAction() {
    document.yourformnamegoeshere.action = "Edit.php";
    document.yourformnamegoeshere.submit();
    }
    function setdeleteAction() {
    document.yourformnamegoeshere.action = "Delete.php";
    document.yourformnamegoeshere.submit();
    
    }
    

    给你的表单起个名字,方法是发布,动作是记录

    <form name="yourformnamegoeshere" method="post" action="">
    

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 2013-05-07
      • 2011-08-28
      • 2017-07-29
      相关资源
      最近更新 更多