javascript - 如何在 symfony2 中提交表单 ajax?

标签 javascript php jquery ajax symfony

我即将使用 Ajax 提交我的表单,我已经使用 POST 成功提交了我的表单,但不知道如何将 Ajax 与 Symfony 结合使用

构建形式

    $builder->add('name', 'text', array('constraints' => array(new NotBlank()), 'attr' => array('placeholder' => 'Name')))
        ->add('gender', 'choice', array('empty_value' => 'Select Gender', 'constraints' => array(new NotBlank()), 'choices' => \AppBundle\Entity\Records::$gender_list, "required" => true))
        ->add('dateOfBirth', 'birthday', array('label' => 'Date Of Birth','required'=>true))
        ->add('image_path', 'file', array('label' => ' ','required'=>false, 'data_class' => null, 'constraints'=>array(new Assert\File(                                             array('mimeTypes'=>$mime_types, 'maxSize'=>'2048k' )))))
        ->add('country_of_birth', 'entity', array('empty_value' => 'Country of Birth',
            'class' => 'AppBundle\Entity\Location',
            'property' => 'country',
            'label' => 'Country of Birth'
        ))
        ->add('religion', 'entity', array('empty_value' => 'Select Religion',
            'class' => 'AppBundle\Entity\Religion',
            'property' => 'name',
            'label' => 'Religion'
        ));

Action

        $success = false;
        $record_rep = new \AppBundle\Entity\Records();
        $form = $this->createForm(new \AppBundle\Form\AddPersonType(), $record_rep);

        if ($this->getRequest()->getMethod() == 'POST') {
            $form->submit($request);
            if ($form->isValid()) {
                $data = $form->getData();
                $file = $data->getImagePath();
                $image = $file->getClientOriginalName();

                $new_image_name = $this->hanldeUpload($image, $file);
                $this->savetoDB($data, $record_rep, $new_image_name);
                $success = true;
            }
        }
        return $this->render('AppBundle:Homepage:add_person_form.html.twig', array('form' => $form->createView(), 'success'=>$success ));
    }

最佳答案

使用 jQuery,使用 serialize() 表单并将其发布到您的路线。

$('#form').submit(function(e) {

    e.preventDefault();
    var url = "{{ path('YOUR_PATH') }}";
    var formSerialize = $(this).serialize();
    
    $.post(url, formSerialize, function(response) {
        //your callback here
        alert(response);
    }, 'JSON');
});

在你的行动中

if ($form->isSubmitted() && $form->isValid()) {

....

  // or return new JsonResponse($anyData);
  return new Response(json_encode(['status'=>'success']));
}

这样应该没问题吧。但您可以在路由中添加一些参数,例如格式、方法等。

关于javascript - 如何在 symfony2 中提交表单 ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29541628/

相关文章:

javascript - 通过 Javascript 将 PHP 数组传递给另一个 PHP 脚本

javascript - 停止多个 setTimeout 实例

图像的 Jquery 按钮集大小问题

javascript - 如何在 Angular JS 投影上对 window.location 进行单元测试分配?

DOM 对象的 Javascript 集合 - 为什么我不能使用 Array.reverse() 反转?

php - 在 PHP 中拆分 IDN URL

javascript - 如何制作一个从底部平滑扩展到视口(viewport) 85% 的 div?

javascript - 当单击某个元素或其子元素时,如何获取该元素的 ID?

javascript - ajax更新完成后如何调用js函数

php - 什么PHP函数来解析这个时间戳42866.32972