我如何返回控制器?

How do I go back in controller?

我有 ID 为 formularregisterformularadresa 的表单,还有一个用于提交 ID 为 comanda 的按钮。

我想一键提交两个表单 (<a type="submit" id="comanda" class="btn btn-default" onclick="submitForms()">Finalizare comanda</a>) 并返回到控制器操作 (zend framework 2) 以验证 post.

中的值

我的JS函数是这样的:

function submitForms() {
    $(document).ready(function() {
        $("#comanda").click(function() {
            $.post($("#formularregister").attr("afisarecos.phtml"), $("#formularregister").serialize() + $("#formularadresa").serialize(), function() {
            });
        });
    });
}

您需要在 Web 上创建一个 AJAX request to your controller action. You'll find a lot of examples,但在 ZF2 中使用 Ajax 最重要的部分是:

  • 在您的 /module/Application/config/module 中启用 ViewJsonStrategy。config.php

    'view_manager' => array(
        // ....
        'strategies' => array(
            'ViewJsonStrategy',
        ),
    ),
    
  • Ajax 在您的 视图中请求

    $.ajax({
        url : 'url/to/yourController/action',
        type: 'POST',
        dataType: 'json',
        data : fromData,
        async: true,
        success:function(data, textStatus, jqXHR)
        {
           //success function
        }
        error : function(xhr, textStatus, errorThrown) 
        {
           // error function
        }
    });
    
  • 在您的控制器操作中

    if ($request->isXmlHttpRequest()) { //ajax call
    
        // your logic here
    
        $jsonModel = new JsonModel(
                       //...
        );
    
        return $jsonModel;
    
    }
    

有关详细信息,请参阅此 simple example (Code + Demo)