M
M
Mick Jagger2016-01-29 23:24:21
JavaScript
Mick Jagger, 2016-01-29 23:24:21

How to submit a form via AJAX in Symfony2?

How to connect AJAX submit to Twig form?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2016-01-29
@R0dger

Javascript if there are other options, I'll be happy to hear them.

K
Konstantin Grachev, 2016-01-30
@Preemiere

You describe in JavaScript sending to the controller for ajax.
Or, if the controller accepts not only ajax, you can check for XmlHttpRequest and return JSON.

$form = $this->createCreateForm($entity);
$form->handleRequest($request);

if ($form->isValid()) {
    $em->persist($entity);
    $em->flush();

    if ($request->isXmlHttpRequest()) {
        return new JsonResponse([
            'id' => $entity->getId(),
        ]);
    }

    return $this->redirectToRoute('route');
}

J
jaxel, 2016-01-31
@jaxel

i do via malsup.com/jquery/form ajaxSubmit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question