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
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.