使用Symfony,我在后端创建了一个表单。我有一个提交按钮,用于保存表单。我需要向应该发送表单数据的表单添加另一个链接(除了提交按钮),例如提交按钮。通过此链接,我将通过控制器中的另一种方法处理提交的数据。
我已经尝试了link_to('Link', '/backend_dev.php/question/edit?id='.$question->getId(), array('post' => true))
和link_to('Link', '/backend_dev.php/question/edit?id='.$question->getId(), array('method' => 'post'))
,但这些函数不发送表单数据。
控制器
public function executeSave(sfWebRequest $request)
{
$this->form = new SomeForm(SomePeer::retrieveByPk($request->getParameter('id')));
if($request->isMethod('post'))
{
$this->form->bind($request->getParameter('some'), $request->getFiles('some'));
if ($this->form->isValid())
{
$some = $this->form->save();
$this->redirect('some/edit?id='.$some->getId());
}
}
}
public function executeLink(sfWebRequest $request)
{
// Like executeSave I need form data here
}
将 jQuery 与链接上的 onClick 事件一起使用,该事件将更改表单的 action
属性并提交它。假设链接具有 id myLink
并且格式为 id myForm
:
$('#myLink').click(function(e){
$('#myForm').attr('action','<?php echo '/backend_dev.php/question/edit?id='.$question->getId()?>';
$('#myForm').submit();
e.preventDefault();
});