Nad Nad - 4 months ago 37
PHP Question

symfony send form request without submit

I want to send request using just choice type, and without submit button, i tried with javascript but no result

My form code:

$form = $this->createFormBuilder()
->add('language', 'choice', array(
'choices' => array(
'ar' => 'Arabic',
'fr' => 'French',
'es' => 'Espagnol',
'nl' => 'Dutch',
'ja' => 'Japanese',
'en' => 'English',
'ko' => 'Korean',
'it' => 'Italian',
'ru' => 'Russian',
),
'required' => false,
'placeholder' => 'Click to Choose Language..',
'attr' => array(
'id' => 'field',
'class' => 'controls',
'data-rel' => 'chosen'
)
))
->getForm();


My twig is as follows:

<form action="{{ path('test_test') }}" method="POST" {{ form_enctype(form) }}>
{{ form_widget(form.language, { 'attr': {'class': 'select2'} }) }}
</form>


The javascript code that i used is :

<script type="text/javascript">
$('#field').change(function() {
$(this).closest('form').trigger('submit');
});
</script>

DOZ DOZ
Answer

You can use Jquery submit function like this:

jQuery(function($) {
   $(".select2").change(function() { //put class or id of your select input
      $('#id_of_your_form').submit();
   });
});

EDIT:

If you are using select2 plugin (you have examples here) :

jQuery(function($) {
     $('.select2').select2()
     .on("change", function(e) {
          $('#id_of_your_form').submit();
     })
});

And then you can get POST data sending in your Symfony Controller like this :

public function getDataAction(Request $request)
{
   dump($request->request->get('name of your select input');exit;
}