0Day Forums
set php radio button checked by default - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: CMS (https://0day.red/Forum-CMS)
+---- Forum: WordPress (https://0day.red/Forum-WordPress)
+---- Thread: set php radio button checked by default (/Thread-set-php-radio-button-checked-by-default)



set php radio button checked by default - misti892 - 07-27-2023

I have two radio buttons in my program but when i run it none of them is checked, I want one of them to be checked by default,
how can i achieve this ?

<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' : ''; ?> />Off</li>

I want the 'On' button to be checked when i open the page for the first time


RE: set php radio button checked by default - renelle665 - 07-27-2023

Something like this:

<li><input type="radio" name="r1" value="o" onClick="submit();" CHECKED/>On</li>


RE: set php radio button checked by default - kallilsgkewf - 07-27-2023

What about this:

<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo (!$_SESSION['r1'] || $_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?> />On</li


RE: set php radio button checked by default - anthonyxr - 07-27-2023

Here you go:

<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] != "p") ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' : ''; ?> />Off</li>



RE: set php radio button checked by default - phonomimic946444 - 07-27-2023

Well, in order for the On button to checked, the `$_SESSION['r1']` must be equal to "o". Have you checked the value of `$_SESSION['r1']`?