简体   繁体   English

Magento 2 - 如何在 phtml 文件中传递输入值以阻止 class?

[英]Magento 2 - How to pass input value in phtml file to block class?

I am trying to overwrite a Magento module block with my customized code, both in phtml file and a php class in Block.我正在尝试用我的自定义代码覆盖 Magento 模块块,包括 phtml 文件和块中的 php class。 In my phtml file, I added an input field and I would like to pass the value of the input to the Block class, so I could be able to use the value to update the database.在我的 phtml 文件中,我添加了一个输入字段,我想将输入的值传递给块 class,这样我就可以使用该值来更新数据库。

So far, I have created a button and an onClick php function called by the button, but I have no idea how to pass the input value to that function. So far, I have created a button and an onClick php function called by the button, but I have no idea how to pass the input value to that function. I have tried to use the document.getElementById , but it is not working.我曾尝试使用document.getElementById ,但它不起作用。

The php function is: php function 是:

public function updatePayment($paidAmount)
{
    $currentPaid = $this->getSource()->getTotalPaid();
    $this->getOrder()->setTotalPaid($paidAmount + $currentPaid)->save();
}

phtml file input HTML: phtml文件输入HTML:

<input id="update-payment-input" type="text" style="text-align:right;" value="<?= $block->setDefaultAmountForPayment() ?>">
<script>
    require(['prototype'], function() {
        document.getElementById("update-payment-submit").addEventListener("click", updateAmount);
        function updateAmount() {
            <?= $block->updatePayment() ?>
            // Here I tried to use document.getElementById to get the input value
        }
    });
</script>

I expect the value to be captured and passed to the function in Block class, but it shows an error saying that the document is undefined.我希望该值被捕获并传递给 class Block中的 function,但它显示一个错误,指出该文档未定义。

Add this code to your HTML file.将此代码添加到您的 HTML 文件中。

<input id="update-payment-input" type="text" style="text-align:right;" value="<?= $block->setDefaultAmountForPayment() ?>" onclick="updateAmount(this.value)">
<script>
    function updateAmount(value) {
        <?= $block->updatePayment(value) ?>
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM