I have 2 following radio boxes in a form,
<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
Use the input with type="radio" to create a radio button. Use the same name for multiple radio buttons to define a radio group. Get the value of a checked radio via the $_POST (or $_GET ) variable, depending on the request method.
Add type property with submit type="submit" to button tag this will submit the form then receive the form inputs value in php script with super global variable $_POST['input name'] or $_GET[] wdepends on from action property value by default GET.
1) The value of the radio button is saved in $_POST
only if any of the choices was selected.
if (isset($_POST['radio'])) // if ANY of the options was checked
echo $_POST['radio']; // echo the choice
else
echo "nothing was selected.";
2) Just check for the value and add checked='checked'
if it matches.
<input type="radio" name="radio" value="yes" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'yes'): ?>checked='checked'<?php endif; ?> /> Yes
<input type="radio" name="radio" value="no" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'no'): ?>checked='checked'<?php endif; ?> /> No
<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
u get radio value using $_POST['radio'];
simple bro,
<input type="radio" name="radio" <?php if($_POST['radio']=="yes") echo "checked";?> value="yes" class="radio" /> Yes
u have to identify radio box by value man
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With