Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit form ajax in symfony2?

I am about to submit my form Using Ajax,i have successfully submit my form using POST but don't know how to use Ajax with Symfony

builform

    $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 ));
    }
like image 631
Amr Avatar asked Apr 09 '15 14:04

Amr


People also ask

Can we submit form using AJAX?

We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.

What is AJAX form submission?

AJAX form submitting allows you to send data in the background, eliminating the need to reload websites to see the updates. This makes the user experience much smoother.

What is the difference between AJAX and form submit?

A standard form submit sends a new HTTP request (POST or GET) and loads the new page in the browser. In Ajax, the data is sent to the server (POST or GET) in the background, without affecting the page at all, and the response is then received by javascript in the background, again without affecting the page at all.


1 Answers

With jQuery, use serialize() the form and post it to your route.

$('#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');
});

In your action

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

....

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

it must be ok like this. but you can add some parameters like the format, methods etc... in your routing.

like image 186
MouradK Avatar answered Oct 04 '22 15:10

MouradK