目前我在我的index.html.twig文件中有一个新的操作。Symfony:ajax表单加载
<div id="formcontent">
form content goes here
</div>
<a href="{{ path('uni_new') }}">Create a new uni</a>
和我的新的行动看起来像这样
/**
* Creates a new uni entity.
*
* @Route("/new", name="uni_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$uni = new Uni();
$form = $this->createForm('AppBundle\Form\UniType', $uni);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($uni);
$em->flush($uni);
return $this->redirectToRoute('uni_show', array('id' => $uni->getId()));
}
return $this->render('uni/new.html.twig', array(
'uni' => $uni,
'form' => $form->createView(),
));
}
我想通过Ajax请求来完成这个新动作。所以,当用户点击Create a new uni
,而不是去一个新的网址,我希望通过ajax调用加载表单,并替换上面的div的内容,编号为formcontent
。我怎样才能做到这一点?提前致谢。
您显示的PHP代码总体上是OK的,您需要的是以JS为中心的,并且与Symfony无关。你应该在那里添加你的JS代码。 – lsouza