currently I have a new action in my index.html.twig file.
<div id="formcontent">
form content goes here
</div>
<a href="{{ path('uni_new') }}">Create a new uni</a>
and my new action looks like this
/**
* 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(),
));
}
I want this new action to be done by ajax request. So when a user click on Create a new uni, instead of going to a new url, I want the form to be loaded by ajax call and replace the content of the above div with id formcontent. How can I do this? Thanks in advance.