How can I save data in the database with a form that is loaded via Ajax in Symfony 4?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I save data in the database with a form that is loaded via Ajax in Symfony 4?



homepage.html.twig


<a class="load-form" href=" path('article_load_form', slug:page.slug) ">Load my beautiful form</a>

<div class="show-form">form will appear here</div>

<script>
$( ".load-form" ).on( "click", function(e)

e.preventDefault();

var $link = $(e.currentTarget);

$.ajax(method:'POST', url: $link.attr('href')).done(function(data)
$('.show-form').html(data.output);

);
);
</script>



myController.php


/**
* @Route("/pages/slug/heart", name="article_load_form", methods="POST")
*/


public function loadForm($slug, Request $request)

$id = 9;

$item = new User();

$item= $this->getDoctrine()->getRepository(User::class)->find($id);
$form = $this->createFormBuilder($item)
->add('username', TextType::class, array('attr' => array('class' => 'form-control')))
->add('email', EmailType::class, array('attr' => array('class' => 'form-control')))
->add('is_active', HiddenType::class)
->add('plainPassword', RepeatedType::class, array('type' => PasswordType::class,'invalid_message' => 'The password fields must match.','options' => array('attr' => array('class' => 'password-field')),'required' => false,'first_options' => array('label' => 'Passwort', 'attr' => array('class' => 'form-control')),'second_options' => array('label' => 'Passwort wiederholen', 'attr' => array('class' => 'form-control')),))
->add('cancel', ButtonType::class, array('label' => 'Abbrechen','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar close_h')))
->add('save', SubmitType::class, array('label' => 'Speichern','attr' => array('class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')))
->getForm();
$form->handleRequest($request);

$response = new JsonResponse(
array(
'message' => 'Success',
'output' => $this->renderView('form.html.twig',
array(
'entity' => $item,
'form' => $form->createView(),
))), 200);

return $response;

if($form->isSubmitted() && $form->isValid())
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();





My form is loaded when I click on "Load my beautiful form" with all the data from the database for each field. So this is working fine. BUT when I then change the data inside the form (for example the username from "leo" to "alan") and click the save button, then no data is stored in the database (the username is still "leo").



In the profiler the POST parameters are correct. But no forms were submitted for this request.





Please, I would be very happy if you tell my why downvote. Because I really don't find a solution. And I posted my approach. There are no error messages, so I could not post any errors.
– Jarla
Aug 10 at 15:04




1 Answer
1



The action will return a response before the following block will be reached


if($form->isSubmitted() && $form->isValid())
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();



So nothing will be stored in the DB.



then let's see what you are trying to do,
you try to get your beautiful from with ajax and put it to your load-form, so you need at first to use a get method, method:'GET',



after that you need an other ajax method to load your data to the server with post


$('#submit-my-beautiful-form').click(function(e)
e.preventDefault();
var form = $(this).closest('form');
var formData = form.serialize();
alert(formData);
$.ajax(
method:'POST',
url: $link.attr('href'),
data: formData,
success: function(data)
alert(data);

);
)



ps: your form data will be serialized and affected to formData variable and then loaded to the server
ps: you need to add an id to your save button :


->add('save', SubmitType::class, array('label' => 'Speichern','attr' => array('id' => 'submit-my-beautiful-form', 'class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')))



ps: dont forget to persist your $item object


$entityManager->persist($item);





Thank you. So what can I do to store it?
– Jarla
Aug 10 at 14:33





I tried to set this block before the json response block. But it did not solve the problem
– Jarla
Aug 10 at 14:34





you need to persist your object before flushing it, because you've just instantiated it, $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($item); $entityManager->flush();
– Jabri Amine
Aug 10 at 15:13






Thank you! I tested $entityManager->persist($item); But still no data ist stored
– Jarla
Aug 10 at 15:15


$entityManager->persist($item);





No forms were submitted for this request.
– Jarla
Aug 10 at 15:19






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard