【问题标题】:write a function which calls from phtml file in magento在magento中编写一个从phtml文件调用的函数
【发布时间】:2020-01-10 12:23:57
【问题描述】:

我是 magento 的新手, 我在 magento 1.9 中创建了一个 phtml 模板文件。 我那个 phtml 我已经创建了一个 html 表单。我想在提交该表单后调用一个函数。我有点困惑我应该如何编写一个函数我应该创建一个控制器或一个块还是在同一个 phtml 中我已经编写了一个函数。 请指导我正确 `

            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 content-row">
                <h2>PLEASE ENTER</h2>
            </div>

            <div class="col-lg-12">
                <div class="col-sm-4 col-md-4 col-lg-4 col-xs-4">
                    <div class="form-group">
                        <label>name</label><br><br> 
                        <input class="form-control" maxlength="2">
                    </div>
                </div>

`

【问题讨论】:

    标签: php magento


    【解决方案1】:

    有几种方法可以做到这一点:https://www.w3schools.com/jsref/event_onsubmit.asp

    这是一个简单的示例,您可以在表单提交时运行 JS。

    <form name="yourform" onsubmit="your_script()" >
        <input type="text" name="text"/>
      <input type="submit" name="Submit" value="Submit"/>
    </form>
    
    function your_script() {
    //your script here
    }
    

    【讨论】:

    • 谢谢。在 phtml.html 中在 html 表单下方编写一个函数是一种好方法吗?因为它将成为 phtml 文件中的很多代码
    • 您实际上可以将其添加到您的 .xml 文件中 action> OR skin_jspath/thescript.js
    【解决方案2】:

    它与MVC pattern 链接。简而言之,您需要在控制器上编写一个新操作。

    例如,如果您的表单要发送电子邮件,您可以在负责显示表单的控制器中编写一个名为 sendAction() 的新方法。在这种方法中,您将检查您的表单并在必要时发送电子邮件。

    块保存块本身的逻辑(例如检索将显示的数据)

    phtml 文件仅用于显示,即使您可以在 Magento 文件中看到 phtml 有逻辑,但这是一个坏习惯,并且有历史原因。

    最后,如果您的表单逻辑由 Magento 控制器持有,您需要 override the controller 并且不要触摸 Core

    【讨论】:

    • 非常感谢。但实际上我的表单是一个弹出窗口,它将直接显示在主页上,因此没有任何其他文件夹,如控制器块。这就是为什么我很困惑如何直接创建控制器以及如何链接
    • 在这种情况下,使用您需要的控制器创建您自己的模块并使用 geturl 函数进行操作。可以用ajax管理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多