Styles2304 Posted July 5, 2007 Share Posted July 5, 2007 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 https://forums.phpfreaks.com/topic/58499-solved-retrieving-data-and-radio-butons-dont-know-how-to-word-this/ Share on other sites More sharing options...
Barand Posted July 5, 2007 Share Posted July 5, 2007 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 https://forums.phpfreaks.com/topic/58499-solved-retrieving-data-and-radio-butons-dont-know-how-to-word-this/#findComment-290191 Share on other sites More sharing options...
Styles2304 Posted July 5, 2007 Author Share Posted July 5, 2007 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 https://forums.phpfreaks.com/topic/58499-solved-retrieving-data-and-radio-butons-dont-know-how-to-word-this/#findComment-290206 Share on other sites More sharing options...
Styles2304 Posted July 5, 2007 Author Share Posted July 5, 2007 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 https://forums.phpfreaks.com/topic/58499-solved-retrieving-data-and-radio-butons-dont-know-how-to-word-this/#findComment-290555 Share on other sites More sharing options...
Barand Posted July 5, 2007 Share Posted July 5, 2007 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 https://forums.phpfreaks.com/topic/58499-solved-retrieving-data-and-radio-butons-dont-know-how-to-word-this/#findComment-290650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.