2015-04-09 14 views
8

Ich bin über meine Form mit Ajax einreichen, ich habe meine Form mit POST erfolgreich einreichen aber nicht wissen, wie Ajax zu verwenden, um mit SymfonyWie man Formular Ajax in symfony2 einreicht?

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' 
     )); 

Aktion

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

Antwort

15

Mit jQuery, verwenden das Formular und post es auf Ihre 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 Ihrer Aktion

if ($form->isValid()) { 

.... 

    return new Response(json_encode(array('status'=>'success'))); 
} 

es muss wie dies in Ordnung sein. aber Sie können einige Parameter wie das Format, Methoden usw. in Ihrem Routing hinzufügen.

+0

meine Aktion bereits html so machen, wenn ich Ihre Lösung versucht es immer den Erfolg Wert = false machen, es so muss Muss ich meiner Ansicht nach auch damit umgehen, dass ich Bild habe, also soll dieser Block Ajax durchgeführt werden? – Amr

+0

Sieht so aus, als ob die letzte Zeile des obersten Codeblocks '}) sein sollte;' '' '-HTH –

+1

' return new Antwort (json_encode (array ('status' => 'success')); 'fehlt a abschließende Klammer, sollte 'return new Response (json_encode (array ('status' => 'success')));' -HTH sein –

2

für die Ajax

$("#person").submit(function(e){ 


    var formURL = "{{ path('form') }}"; 
    var formData = new FormData(this); 
    $.ajax({ 
     url: formURL, 
     type: 'POST', 
     data: formData, 
     mimeType:"multipart/form-data", 
     contentType: false, 
     cache: false, 
     processData:false, 
     success: function(data, textStatus, jqXHR) 
     { 

     }, 
     error: function(jqXHR, textStatus, errorThrown) 
     { 
     } 
    }); 
    e.preventDefault(); //Prevent Default action. 
    e.unbind(); 
}); 
$("#person").submit(); 

und für Aktion

if ($request->isXmlHttpRequest()) { 

.... 

return new Response(json_encode(array('status'=>'success')); 
} 
Verwandte Themen