Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Symfony 4中通过Ajax加载和提交表单_Php_Ajax_Forms_Symfony_Entitymanager - Fatal编程技术网

Php 如何在Symfony 4中通过Ajax加载和提交表单

Php 如何在Symfony 4中通过Ajax加载和提交表单,php,ajax,forms,symfony,entitymanager,Php,Ajax,Forms,Symfony,Entitymanager,我正在通过Ajax将表单加载到我的页面: $('.table tbody').on( 'click', '.edit-item', function (e) { var id = $(this).attr("data-id"); e.preventDefault(); var $link = $(e.currentTarget); $.ajax({ method:'POST', data: { "id": id }, url: $lin

我正在通过Ajax将表单加载到我的页面:

$('.table tbody').on( 'click', '.edit-item', function (e) {
  var id = $(this).attr("data-id");
  e.preventDefault();
  var $link = $(e.currentTarget);
  $.ajax({
    method:'POST',
    data: {
      "id": id
    },
    url: $link.attr('href')
  }).done(function(data){
    $('.form-output').html(data.output);

    $('#form_save').on( 'click', function (e) {
      e.preventDefault();
      var form = $(this).closest('form');
      var formData = form.serialize();
      alert("save button clicked");
      $.ajax({
        method:'POST',
        url:'{{ path('edit_form', { 'slug': page.slug }) }}',
        data: formData,
        success: function(data){

          alert("success");
        }
      });
    });

  });
});
Controller.php:

 /**
  * @Route("/pages/{slug}/edit", name="edit_form", methods={"POST", "GET"})
  */


  public function toggleArticleHeart($slug, Request $request){

    if($request->request->get('id')){

      $id = $request->request->get('id');
      $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('id' => 'submit-my-beautiful-form','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;

        } else {

          $data = $request->request->get('data');
          $entityManager->persist($data);
          $entityManager = $data->getDoctrine()->getManager();
          $entityManager->flush();
          $response = new Response();
          $response->send();
        }
     }
我收到消息“保存按钮已点击”,但没有消息“成功”

还有一个Ajax 500错误:

未捕获的PHP异常ErrorException:“注意:未定义的变量: “实体管理器”


您需要在控制器中定义并初始化$entityManger(即,
$entityManager=$this->getDoctrine()->getManager();
),在使用它之前,请按如下方式更改控制器:

/**
* @Route("/pages/{slug}/edit", name="edit_form", methods={"POST", "GET"})
*/

public function toggleArticleHeart($slug, Request $request){

   if($request->request->get('id')){

     $id = $request->request->get('id');
     $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('id' => 'submit-my-beautiful-form','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;

    } else {

      $entityManager = $this->getDoctrine()->getManager();
      $data = $request->request->get('data');
      // Create Entity object and set it property with setter and then do persist on entity object 
      $entityManager->persist($data);
      $entityManager->flush();
      $response = new Response();
      $response->send();
    }
 }

您需要在控制器中定义并初始化$entityManger(即,
$entityManager=$this->getDoctrine()->getManager();
),在使用它之前,请按如下方式更改控制器:

/**
* @Route("/pages/{slug}/edit", name="edit_form", methods={"POST", "GET"})
*/

public function toggleArticleHeart($slug, Request $request){

   if($request->request->get('id')){

     $id = $request->request->get('id');
     $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('id' => 'submit-my-beautiful-form','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;

    } else {

      $entityManager = $this->getDoctrine()->getManager();
      $data = $request->request->get('data');
      // Create Entity object and set it property with setter and then do persist on entity object 
      $entityManager->persist($data);
      $entityManager->flush();
      $response = new Response();
      $response->send();
    }
 }

我测试了您的代码,但收到错误消息:
EntityManager#persist()希望参数1是实体对象,给定NULL。
根据您的代码,您正在尝试保存get参数的
data
键中的数据。请检查我的更新答案,您需要创建要持久化的实体对象,并使用setters设置其值,然后在
持久化
函数中使用实体对象作为参数。希望这对你有帮助。非常感谢。我测试了你的新更新。仍然存在相同的错误:
EntityManager#persist()希望参数1是实体对象,给定为NULL。
数据的格式可能不正确
var formData=form.serialize()持久化$data时出现问题。这是$data的输出:我测试了您的代码,但收到错误消息:
EntityManager#persist()希望参数1是实体对象,给定NULL。
根据您的代码,您正在尝试保存get参数的
data
键中获得的数据。请检查我的更新答案,您需要创建要持久化的实体对象,并使用setters设置其值,然后在
持久化
函数中使用实体对象作为参数。希望这对你有帮助。非常感谢。我测试了你的新更新。仍然存在相同的错误:
EntityManager#persist()希望参数1是实体对象,给定为NULL。
数据的格式可能不正确
var formData=form.serialize()持久化$data时出现问题。这是$data的输出: