doushi6864 2013-01-30 21:13
浏览 44
已采纳

通过Ajax调用加载Zend表单

I have a requirement to load a form into a page dynamically, with the potential for multiple instances of this form. Validating and saving is not a problem, but I cannot seem to pass back the actual form content / mark-up from the Controller. When testing with simple values (id: 1) I can return the data successfully, but the actual form is always returned as undefined if the datatype is not set, or [Object object].

Controller:

public function addPermissionFormAction()
{
    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->layout->disableLayout();
    //data from ajax call is successfully received
    $username = $this->getRequest()->getParam('user');
    //...Code to get $pages...
    $form = new Pds_Wizard_SubForm_Externals($pages);

    if(!isset($form)) $form = false;
    //I have run XDebug to here and confirmed that the form is built successfully.
    echo Zend_Json::encode(array('form' => $form));
}

My Ajax Call:

$('#get-player-permissions').click(function() {
    if($('#test-test').val().length > 0) {
        $.ajax({
        url: '/pds/external-user/add-permission-form',
        data: { user: $('#test-test').val() },
        dataType: 'json',
        contentType: "application/json",
        success: function(data){
            alert(data.form); // [Object object]
            $("#wizardContainer").html(data.form); //empty, no form content
        }
    })
    } 
});

I have tried this without even adding the dataType or contentType fields for sending simple values, and have no issue getting those through. With the form, however, I will get a null or undefined in this case.

Any advice is appreciated, including more ideal methods of dynamically loading a form. I do not use Ajax calls very often, so if this is a simple mistake I apologize. Thank you in advance!

  • 写回答

1条回答 默认 最新

  • douzhimei8259 2013-01-30 21:43
    关注

    You can't set "no render" in this case. You need to have a view "add-permission-form.phtml" (or another view specified) to format and display the HTML code

    Controller :

    public function addPermissionFormAction()
    {
        $this->_helper->layout->disableLayout();
    
        //data from ajax call is successfully received
        $username = $this->getRequest()->getParam('user');
        //...Code to get $pages...
        $form = new Pds_Wizard_SubForm_Externals($pages);
    
        if(!isset($form)) $form = false;
        //I have run XDebug to here and confirmed that the form is built successfully.
        echo Zend_Json::encode(array('form' => $form));
    
        //Assign the form to a variable view
        $this->view->form = $form;
    }
    

    View : add-permission-form.phtml :

    <?php echo $this->form ;?>
    

    Ajax call :

    No comment, your code look cool

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作