Jump to content

POST value to radio button input FAILS


kristo5747

Recommended Posts

I'm coding a page form and to prevent its fields to be reset, I have to echo the POST values back into their respective form fields.

 

It is working well for text inputs but I am having problem with four radio button inputs. Echo-ing the POST values back does not work: the respective form fields are always reset, blanked out...no matter what!!

 

This is what I wrote...

...<td><fieldset> <legend>Frequency: </legend> 
<input type="radio" id="rad0" tabindex="8" name="Frequency" value="One Time" <?php echo $ontime_status; ?>>One Time<br> 
<input type="radio" id="rad1" tabindex="9" name="Frequency" value="Daily" <?php echo $dly_status; ?>>Daily<br> 
<input type="radio" id="rad2" tabindex="10" name="Frequency" value="Weekly" <?php echo $wky_status; ?>>Weekly<br> 
<input type="radio" id="rad3" tabindex="11" name="Frequency" value="Monthly" <?php echo $mthly_status; ?>>Monthly<br> 
<input type="radio" id="rad4" tabindex="12" name="Frequency" value="Ongoing" <?php echo $ongoing_status; ?>>Ongoing<br> 
</fieldset></td>...

 

... and my code...

...if (isset($_POST['Frequency'])) { 
        $selected_radio = $_POST['Frequency'];         
        if($selected_radio == 'One Time') { 
            $ontime_status = 'checked'; 
        } elseif($selected_radio == 'Daily') { 
            $dly_status = 'checked'; 
        } elseif($selected_radio == 'Weekly') { 
            $wky_status = 'checked'; 
        } elseif($selected_radio == 'Monthly') { 
            $mthly_status = 'checked'; 
        } elseif($selected_radio == 'Ongoing') { 
            $ongoing_status = 'checked'; 
        } 
    }... 

 

What I am doing wrong?? Can someone please tell me?!

Link to comment
https://forums.phpfreaks.com/topic/198033-post-value-to-radio-button-input-fails/
Share on other sites

radio buttons - only one can be checked.  You would use the checked attribute for the correct one instead of the value from the post data.

e.g.

<input type="radio" id="rad0" tabindex="8" name="Frequency" value="One Time" CHECKED>One Time<br> 

so...

<input type="radio" id="rad0" tabindex="8" name="Frequency" value="One Time" <?php echo ($_POST['Frequency']=="One Time")?"CHECKED":""; ?>>One Time<br> 
<input type="radio" id="rad1" tabindex="9" name="Frequency" value="Daily" <?php echo ($_POST['Frequency']=="Daily":"CHECKED":"";  ?>>Daily<br> 
<input type="radio" id="rad2" tabindex="10" name="Frequency" value="Weekly" <?php echo ($_POST['Frequency']=="Weekly")?"CHECKED":""; ?>>Weekly<br> 
<input type="radio" id="rad3" tabindex="11" name="Frequency" value="Monthly" <?php echo ($_POST['Frequency']=="Monthly")?"CHECKED":""; ?>>Monthly<br> 
<input type="radio" id="rad4" tabindex="12" name="Frequency" value="Ongoing" <?php echo ($_POST['Frequency']=="Ongoing")?"CHECKED":"";  ?>>Ongoing<br> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.