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