Form Submission on radiobutton click

Hello Experts,

I have a form in which user will choose one option from the radio button list. I want to submit the form instantly, when any radio button is selected/clicked.

Can you guys help me how to do that. I tried below code. But its not working.




<?= $form->field($model, 'user_selection')->radioList(['Option1'=>$opt1,'Option2'=>$opt2,'Option3'=>$opt3], ['onclick'=>'this.form.submit()'])->label(false); ?>



Thanks in advance

Regards,

Sid

to reference the form you need to first find the form, here is your code slightly modified


// don't forget to change the form-id to match the id of your form

'onclick' => 'document.getElementById("form-id").submit()'

Hello alrazi,

Thanks for your input. It works in my end.

Can this threat be tagged as "Solved"??

Regards,

Sid

Hello Alrazi,

I have been working with the hint provided by you. It works fine as long as I select the radio button and submit it with a submit button.

In my sceanario, I have a radiolist which will shuffle in each submission.

View Code as below:

Working fine with radio list value shuffling:




<?php function shuffle_arr($arr){

 $keys = array_keys($arr);

 shuffle($keys);

 foreach($keys as $key) {

  $shuffeled[$key] = $arr[$key];

 }

 return $shuffeled;

}?>


<div id= "content">


            <?php $form = ActiveForm::begin([

                        'id' => 'form-test'])?>

     

    <div class="col-sm-6"> 

    <div class="qr" id="rlist"> 

	<?= $form->field($model, 'name')->radioList(shuffle_arr([$x=>$x,$y=>$y]))->label(false); ?>

         

        </div>

<?= Html::submitButton('Test', ['class' => 'btn btn-success', 'name' => 'test-button']) ?> 

</div>

<?php ActiveForm::end() ?>

</div>



But when I am putting onclick event with below code, it behaviors strangely. In this case, it takes the value when I click exactly on the rounded radio button. But if I click on the value, it can not save the respective value. It gets the null value.


<?php function shuffle_arr($arr){

 $keys = array_keys($arr);

 shuffle($keys);

 foreach($keys as $key) {

  $shuffeled[$key] = $arr[$key];

 }

 return $shuffeled;

}?>


<div id= "content">


            <?php $form = ActiveForm::begin([

                        'id' => 'form-test'])?>

     

     <div class="col-sm-6"> 

<div class="qr" id="rlist"> 

		


         <?= $form->field($model, 'name')->radioList(shuffle_arr([$x=>$x,$y=>$y]),['onclick'=>'document.getElementById("form-test").submit()'])->label(false); ?>

         

        </div>

</div>

<?php ActiveForm::end() ?>

</div>



Can you guide me, where I am going wrong?

Your help really appreciated.

Regards,

Sid