Jump to content

[SOLVED] Retrieving Data and Radio Butons (Don't know how to word this)


Styles2304

Recommended Posts

I have a PHP form that retrieves saved data from an array form and displays it in the input fields. I display the input field data like this:

 

<?php
echo '<input type="text" size="20" name="hosturl" value="'.$info['hosturl'].'">'
?>

 

The only problem I'm having now is that I don't know how to display radio button data. It's stored and recalled but I don't know how to make the list of radio buttons respond.

 

Any ideas?

try

<?php

$myButtonVals  = array(
                     'a' => 'button A',
                     'b' => 'button B',
                     'c' => 'button C'
                );
                
$info = 'b';        // value from database

foreach ($myButtonVals as $val => $text)
{
     $chk = $info == $val ? 'checked' : ''; // should it be checked?
     echo "<input type='radio'  value='$val' $chk /> $text <br/>";
}
?>

Just so I have a better understanding . . . what, in that code, is actually checking the radio button?

 

Also, due to the content I have around the radio buttons, I can't really have just an entire block of PHP . . . at least not without rewriting what I have and I'd really like to not do that seeing as this is the final piece of the puzzle.

 

Here is my radio button setup

1:<input type="radio" value="1" name="mrate1"> 2:<input type="radio" value="2" name="mrate1"> 3:<input type="radio" value="3" name="mrate1"> 4:<input type="radio" value="4" name="mrate1"> 5:<input type="radio" value="5" name="mrate1">

 

How would I apply that to what I have?

Ok well . . . I know this is more work but I ended up doing this:

 

1:<?php
          if ($info['mrate1'] == 1)
          {
            echo '<input type="radio" value="1" name="mrate1" checked>';
          }else{
            echo '<input type="radio" value="1" name="mrate1">';
          }
         ?>

 

Took a little longer and it's a little less efficient I suppose but with copy and paste, it wasn't too bad.

Even easier with that setup - you don't need the array.

 

<?php
$info['mrate1'] = '3';        // value from database

for ($val=1; $val <= 5; $val++)
{
     $chk = $info['mrate1'] == $val ? 'checked' : '';                 // should it be checked?
     echo "$val:<input type='radio'  value='$val' $chk />  ";
}
?>

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.