stephenl Posted August 4, 2008 Share Posted August 4, 2008 Hi, I have a group of radio buttons, formated as a form inorder to store a numeric value in a MySQL database.... The form works OK when storing the didgit... My problem is that I also need to read the database and display the numeric value as a radio button selection, ie the reverse of storing the data, inorder to accept modifications to the original data Any suggestions would be appreciated Sample Radio Button Group Form :- <form id="form1" name="form1" method="post" action=""> <table width="200"> <tr> <td><label> <input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" /> Button1</label></td> </tr> <tr> <td><label> <input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1" /> Button2</label></td> </tr> <tr> <td><label> <input type="radio" name="RadioGroup1" value="3" id="RadioGroup1_2" /> Button3</label></td> </tr> </table> </form> Thank you Stephen Quote Link to comment https://forums.phpfreaks.com/topic/118062-restoring-radio-button-groups/ Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 Okay, pretty easy. Should be in PHP Help, unless I misunderstood you. So you want the selected radio button to be checked when you re-visit the page? First you need to grab the value. //Change this query to the appropriate table and change the WHERE clause to suit your needs. $query = mysql_query("SELECT * FROM `table` WHERE `whatever`='somevar'") or die("Error: ".mysql_error()); $data = mysql_fetch_array($query); $selected_radio = $data['radio_field']; You can that with mysql_result() if you want, whatever works. Now. Now we can make an array of all the radios and store selected='selected' in the right one. $radios = array("","",""); switch($selected_radio){ case 1: $radios[0] = "selected='selected'"; break; case 2: $radios[1] = "selected='selected'"; break; case 3: $radios[2] = "selected='selected'"; break; } Now that we have our three radio values in, only one will contain selected='selected' and that one will be the previously selected value. So your form now becomes this: <form id="form1" name="form1" method="post" action=""> <table width="200"> <tr> <td><label> <input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" <?php echo $radios[0]; ?>/> Button1</label></td> </tr> <tr> <td><label> <input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1" <?php echo $radios[1]; ?>/> Button2</label></td> </tr> <tr> <td><label> <input type="radio" name="RadioGroup1" value="3" id="RadioGroup1_2" <?php echo $radios[2]; ?>/> Button3</label></td> </tr> </table> </form> Hope that makes sense and you can put some of this to use. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/118062-restoring-radio-button-groups/#findComment-607385 Share on other sites More sharing options...
stephenl Posted August 4, 2008 Author Share Posted August 4, 2008 Hi Thank you for your reply (sorry about the wrong forum ).... I used your suggestion, however the "selected='selected'"; does not seem to work with IE7 ??... I replaced this with "checked='checked'"; and everything is fine.... Is this a good choice ??...will it be backward compatiable ?? Thank you Stephen Quote Link to comment https://forums.phpfreaks.com/topic/118062-restoring-radio-button-groups/#findComment-607498 Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Share Posted August 5, 2008 Yeah use checked='checked' Was just me getting confused with select boxes and checkboxes. Quote Link to comment https://forums.phpfreaks.com/topic/118062-restoring-radio-button-groups/#findComment-608250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.