Zend framework2 ZF 2使用Ajax提交表单
我在ZF2中搜索了很长时间的一些ajax表单示例,但是没有明确的教程,如果有人能帮助我,我会考虑的我正试图使用ajax调用控件的操作zend framework根据布局模板呈现(输出)HTML,您可以假设每个操作内容都是片段,而不是包含在布局中 也就是说,要获得AJAX响应,只需禁用AJAX调用的布局,这看起来很简单,好吗 那么现在我们在哪里添加布局禁用代码,您可以在调用任何操作之前执行此操作,将此代码包含在application Module.php中,我们将附加一个事件来检查调用是否来自AJAXZend framework2 ZF 2使用Ajax提交表单,zend-framework2,Zend Framework2,我在ZF2中搜索了很长时间的一些ajax表单示例,但是没有明确的教程,如果有人能帮助我,我会考虑的我正试图使用ajax调用控件的操作zend framework根据布局模板呈现(输出)HTML,您可以假设每个操作内容都是片段,而不是包含在布局中 也就是说,要获得AJAX响应,只需禁用AJAX调用的布局,这看起来很简单,好吗 那么现在我们在哪里添加布局禁用代码,您可以在调用任何操作之前执行此操作,将此代码包含在application Module.php中,我们将附加一个事件来检查调用是否来自AJ
public function onBootstrap(EventInterface $e){
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'disableLayout'), 100);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
在同一Module.php下添加一个函数
public function disableLayout(EventInterface $event){
$result = $event->getResult();
if ($result instanceof \Zend\View\Model\ViewModel) {
$result->setTerminal($event->getRequest()->isXmlHttpRequest());
//setTerminal(true) disable the layout
}
}
这是一种方法,还有更多的方法,如果您不想禁用所有请求的布局,您可以简单地设置setTerminal(true)来获取任何需要由ajax请求调用的操作的当前viewmodel,这只是一个有助于朝着正确方向移动的示例?只需使用一些js库发送ajax请求并相应地解析响应。如果已正确设置路由,则可以调用任何控制器操作。就像你访问普通的url一样。您可以根据需要发回JSON响应或HTML响应。my action code 公共函数索引(){ //$this->layout('layout/pages')
**我的看法**
$form=$formComment;
)
$form->setAttribute('action','#');
?>
留言
请输入您的姓名?
请发送有效的电子邮件,您需要它来登录
在里面
你的问题到底出在哪里?没有人知道,有这么多可能的方法来回答你的问题…首先考虑一下我的问题引起的兴趣,在添加此修改后,我出现了此错误(参数1传递给Application\Module::onBootstrap()必须是Application\EventInterface的实例,Zend\Mvc\MvcEvent的实例),现在我创建ajax调用函数,URL指向我的操作,并使用$view->setterminal(true)禁用布局报告说,传递给onBootstrap的参数不是事件对象,只需确保您已将我提到的onBootstrap下的行复制到模块中的bootstrap函数。phpi修复了onBootstrap上的问题,现在我发送数据宽度ajax,帖子为空,因此我得到了一个空的插入行,即使url是正确的,h这是我的函数ajax:function saveme(){$.ajax({url:'index',type:'POST',success:function(response){$('formResult').html(response);},error:function(response){$('formResult').html(response);});返回false;}您可以发布您的操作代码和操作视图脚本内容吗?这样,让我知道我可以根据您的ajax请求进一步提供帮助,它应该是您的域/索引我认为对于默认路由,它将指向应用程序/索引/索引,即您的操作是索引控制器中的索引操作,我需要该操作的内容以及相应的视图script(index.phtml)希望您能理解
$this -> id = ( int )$this -> getEvent() -> getRouteMatch() -> getParam('id');
$this -> id_branche = ( int )$this -> getEvent() -> getRouteMatch() -> getParam('id_branche');
$this -> form = new CommentForm();
$this -> form -> get('submit') -> setAttribute('label', 'Add');
$request = $this -> getRequest();
//verifie le type de la requete
if ($request -> isPost()) {
$user = new user();
$comment = new comment();
$blog = new blog();
print 'j';
//Initialisation du formulaire e partir des donnees reues
$this -> form -> setData($request -> getPost());
//$this->form->setData($request->getQuery());
//Ajout des filtres de validation base sur l'objet user,comment
print '2';
//$form->setInputFilter($user->getInputFilter());
//Contrele les champs
if ($this -> form -> isValid()) {
print '3';
$user -> exchangeArray($this -> form -> getData());
$comment -> exchangeArray($this -> form -> getData());
print '4';
$validname = $this->checkuser($this->form->getValue('email'));
if($validname){
$this -> getUserTable() -> saveUser($user);}
else{print 'exist';}
print '5';
$lastUserId = $this -> getUserTable() -> getAdapter() -> getDriver() -> getLastGeneratedValue('id');
$this -> getCommentTable() -> addComment($comment, $lastUserId, $this -> id);
return $this->redirect ()->toRoute ( '#Blog', array ('controller' => 'index',) );
} else {
print 'not set';
}
} else {
$viewm = new ViewModel( array('blog' => $this -> getBlogTable() -> getBlog($this -> id, $this -> id_branche), 'comment' => $this -> getCommentTable() -> fetchJoin($this -> id), 'id' => $this -> id, 'id_branche' => $this -> id_branche, 'formComment' => $this -> form, ));
$viewm -> setTerminal(true);
return $viewm;
}
}
**my view**
$form = $formComment;
)
$form->setAttribute( 'action', '#');
?>
<h4>Leave comment</h4>
<?=$this->form()->openTag( $form )?>
<dl class="zend_form">
<?=$this->formInput ( $form->get( 'id' ) )?>
<div>
<?=$this->formLabel ( $form->get ( 'name' ) )?>
<?=$this->formInput ( $form->get ( 'name' ) )?>
<?=$this->formElementErrors ( $form->get ( 'name' ) )?>
<div id="nameInfo">Please enter your name?</div>
</div>
<div><?=$this->formLabel ( $form->get ( 'email' ) )?>
<?=$this->formInput ( $form->get ( 'email' ) )?>
<?=$this->formElementErrors ( $form->get ( 'email' ) )?>
<div id="emailInfo">Valid E-mail please, you will need it to log
in!</div>
</div>
<div><?=$this->formLabel ( $form->get ( 'site' ) )?>
<?=$this->formInput ( $form->get ( 'site' ) )?>
<?=$this->formElementErrors ( $form->get ( 'site' ) )?>
</div>
<div><?=$this->formLabel ( $form->get ( 'comment' ) )?>
<?=$this->formTextarea ( $form->get ( 'comment' ) )?>
<?=$this->formElementErrors ( $form->get ( 'comment' ) )?>
</div>
<div>
<?=$this->formInput ( $form->get ( 'submit' ) )?>
<?=$this->formElementErrors ( $form->get ( 'submit' ) )?>
</div>
</dl>
<?=$this->form ()->closeTag ( $form )?>