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?

Link to comment
Share on other sites

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/>";
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />  ";
}
?>

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.