Jump to content

Archived

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

redarrow

i am so confussed help radio buttons

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]

Share this post


Link to post
Share on other sites
[code]<?php
if (isset($_POST['submit'])) {
    $likeme = $_POST['like'];
    echo $likeme;
// do whatever else you want ...
}
?>
<form method="POST" action="">
... more form code
[/code]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Barand thank you so much another for the white board whale perfect and cheers andy grate stuff.

Share this post


Link to post
Share on other sites

×

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.