Jump to content

i am so confussed help radio buttons


redarrow

Recommended Posts

advance thank you.

The radio buttion has a yes or no how do you display it back as a yes or no to edit with the users selected choose from the database cheers.

example of radio buttion.

[code]

<form method="POST" action="">
<br>
do you like redarrow
<br>
<input type="radio" name="like" value="yes" checked>yes
<input type="radio" name="like" value="no" >no
<br>
<br>
<input type="submit" name="submit" value="ansaw please">
</form>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/21071-i-am-so-confussed-help-radio-buttons/
Share on other sites

You need to compare the db field value with each of the button values. The one that matches is set as 'checked'.
[code]
<?php
$dbLike = 'yes';    // <---- value from database
?>
<form method="POST" action="">
<br>
do you like redarrow
<br>
<input type="radio" name="like" value="yes" <?php echo $dbLike=='yes' ? 'checked' : '' ?> >yes
<input type="radio" name="like" value="no"  <?php echo $dbLike=='no'  ? 'checked' : '' ?> >no
<br>
<br>
<input type="submit" name="submit" value="answer please">
</form>
[/code]


If you have several possible values then it's easier to use an array and test each value in a loop.

[code]
<?php
$dbLike = 3;      // <---- value from database
?>
<form method="POST" action="">
<br>
do you like redarrow
<br>
<?php
    $likes = array (
        0 => "Who's he?",
        1 => "Really like the guy",
        2 => "He's OK",
        3 => "Neutral feelings",
        4 => "Not a lot",
        5 => "Hate the guy"
    );
    foreach ($likes as $val => $text) {
        $chk = $dbLike==$val ? 'checked' : '';
        echo "<input type='radio' name='like' value='$val' $chk> $text<br>";
    }
?>

<br>
<input type="submit" name="submit" value="answer please">
</form>
[/code]

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.