Below example of code shows how to handle the value of disabled radio button passing through post method.
if we use radio button group as disabled then if we click submit button it will post null value. Instead of getting null value in this example, we used a hiddenfield called “radiobutton_visibility”. When change the radio button selection it will call a java script function and update the hidden variable.
Please look into the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php $radiobutton_visibility=1; $disable = "disabled='disabled'"; //$disable=''; if(isset($_POST['submit'])) { print_r($_POST); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Posting hidden radio button values from a form</title> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div> <div class="pull-left"> <form method="post" action="index.php"> <input type="hidden" value="<?php echo $radiobutton_visibility; ?>" id="radiobutton_visibility" name="radiobutton_visibility" /> <input <?php if($radiobutton_visibility == 1) { echo "checked"; }?> type="radio" value="1" name="radiobuttonvisibility" class="ace radio_visible" <?php echo $disable; ?> > <span class="lbl"> Radio button one </span> </div> <div class="pull-left"> <input <?php if($radiobutton_visibility == 0) { echo "checked"; } ?> type="radio" value="0" name="radiobuttonvisibility" class="ace radio_visible" <?php echo $disable; ?> > <span class="lbl"> Radio button two </span> <input type="submit" value="Submit" id="submit" name="submit"> </form> </div> </div> <script> $('.radio_visible').change(function(){ $('#radiobutton_visibility').val($("input[name='radiobuttonvisibility']:checked").val()); }); </script> </body> </html> |