基于Ajax post请求呈现不同的Zend表单

基于Ajax post请求呈现不同的Zend表单,ajax,zend-framework,Ajax,Zend Framework,我正在尝试使用Ajax post请求根据用户类型显示不同的表单。请求-响应工作正常,但我不知道如何显示表单。例如,如果用户选择父窗体,则我希望显示父窗体,依此类推。我使用的是ZF1.12 public function init() { $contextSwitch = $this->_helper->getHelper('AjaxContext'); $contextSwitch =$this->_helper->contextSwitch();

我正在尝试使用Ajax post请求根据用户类型显示不同的表单。请求-响应工作正常,但我不知道如何显示表单。例如,如果用户选择父窗体,则我希望显示父窗体,依此类推。我使用的是ZF1.12

public function init() {
    $contextSwitch = $this->_helper->getHelper('AjaxContext');
    $contextSwitch =$this->_helper->contextSwitch();
    $contextSwitch->addActionContext('index', 'json')
    ->setAutoJsonSerialization(false)
    ->initContext();
}

public function indexAction() {
    $this->view->user = $this->_userModel->loadUser($userId);
    //if($this->_request->isXmlHttpRequest()) {
        //$this->_helper->layout->disableLayout();
        //$this->_helper->viewRenderer->setNoRender(true);
    if ($this->getRequest()->isPost()){
        $type = $_POST['type'];
        $this->view->userForm = $this->getUserForm($type)->populate(
            $this->view->user
        );
    }
}
这是我在客户方面的资料。我需要在成功部分写些什么

<script type="text/javascript"> 
  $(document).ready(function(){
    $('#userType').on('change', function(){
      var type = $(this).val();
      select(type);
    });
  });

  function select(type) {
    $.ajax({
        type: "POST",
        url: "admin/index/",
        //Context: document.body,
        data: {'type':type},
        data: 'format=json',
        //dataType: "html",
        success: function(data){
         // what to do here?
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {}
    });
}
</script>
<form id="type" name="type" method="post" action="admin/index">
  <select name='userType' id='userType' size='30'>
    <option>admin</option>
    <option>parent</option>
    <option>teacher</option>
  </select>
</form>
<div id="show">
  <?php //echo $this->userForm;?>
</div>

如果ajax请求表单从Zend_表单返回HTML,那么只需在show div中编写HTML即可

在您的视图中,您需要执行以下操作:

echo $this->userForm;
这样,在将响应发送到HTML页面之前,所有必需的HTML都将在服务器端写入。在HTML页面中,您只需使用$'show'.htmldata方法将响应写入正确的位置。您还必须确保在渲染表单时每个表单都有正确的操作


另一个选项是在加载时通过Javascript将所有三个表单隐藏在页面中,并根据JS生成的选择显示正确的表单。这样,您不必从外部源加载数据,如果有人禁用了JS,他仍然可以使用该应用程序。另一方面,此方法将使每页加载大约1/2 KB的数据。

感谢您纠正我的拼写错误:非常感谢您的回复。出于某种原因,$this->getRequest->isPost引起了麻烦,因此我对该位进行了注释,并按照您的建议对表单进行了响应,以便能够捕获ajax响应并将其显示在页面上。我需要在视图文件夹中有一个名为x.ajax.phtml的单独文件,并在该文件中回显表单。我只是提到这一点,因为其他人可能会面临类似的问题。