Search code examples
phpformssessionradio-button

Radio Buttons & Sessions, how to get value and use as a session variable


How would i get the value of a radio button and use that in my session so for example;

<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />

<div class="underage <?= isset($_SESSION['minor']) ? "style=\"display: block;\"" : "style=display: none;\"" ?>">
            <label>Guardian 1</label>
            <input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1');?>" />
            <label>Guardian 2</label>
            <input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2');?>" />
        </div>

as you can see the code calls the name "minor" but even if NO is selected it will still show the div, regardless of the value, i need to pass the value into my isset($_Session['minor']) parameter, how would this be achieved ?


Solution

  • @Xavier: Try --

    <input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
    <input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
    
    <div class="underage"<?php echo isset($_SESSION['minor']) && $_SESSION['minor'] != 'no' ? 'style="display: block;"' : 'style="display: none;"' ?>>
        <label>Guardian 1</label>
        <input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1'); ?>" />
    
        <label>Guardian 2</label>
        <input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2'); ?>" />
    </div>