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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀