2015-04-09 17 views
8

tôi về để nộp mẫu đơn của tôi Sử dụng Ajax, tôi đã thành công nộp mẫu đơn của tôi sử dụng POST nhưng không biết làm thế nào để sử dụng Ajax với SymfonyLàm cách nào để gửi biểu mẫu ajax trong symfony2?

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

Hành động

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

Trả lời

15

Với jQuery, sử dụng serialize() biểu mẫu và đăng lên tuyến đường của bạn.

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

Trong hành động của bạn

if ($form->isValid()) { 

.... 

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

nó phải là ok như thế này. nhưng bạn có thể thêm một số tham số như định dạng, phương thức, v.v ... trong định tuyến của mình.

+0

hành động của tôi đã làm cho html nên khi tôi đã cố gắng giải pháp của bạn nó luôn luôn làm cho giá trị thành công = false, vì vậy nó phải tôi có cần phải xử lý trong quan điểm của tôi, ngoài ra tôi có hình ảnh để ajax chặn này được thực hiện? – Amr

+0

Dường như dòng cuối cùng của khối mã trên cùng phải là '});' thay vì '}' -HTH –

+1

'trả về trả lời mới (json_encode (mảng ('status' => 'success'));' thiếu một phải là 'return new Response (json_encode (mảng ('trạng thái' => 'thành công')));' -HTH –

2

cho 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(); 

và Đối Action

if ($request->isXmlHttpRequest()) { 

.... 

return new Response(json_encode(array('status'=>'success')); 
} 
Các vấn đề liên quan