helpmeplease2 Posted May 27, 2006 Share Posted May 27, 2006 Say I have this....[code]Yes<input type='radio' name='email' value='1'> No<input type='radio' name='email' value='0'><[/code]I want it to look at the spot in my database where the value is equal to either 1 or 0. If it is a 1 in the database then the first radio button should be selected as default, or the second radio to be selected if there is a 0 in the database.Anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/ Share on other sites More sharing options...
poirot Posted May 27, 2006 Share Posted May 27, 2006 There is no quick method, but you can use something like...[code]$pattern = "value='$val'";str_replace($pattern, $pattern . ' checked=checked', $str);[/code]$val is the value retrieved from MySQL, $str the string containing HTML cod.e Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/#findComment-39373 Share on other sites More sharing options...
helpmeplease2 Posted May 27, 2006 Author Share Posted May 27, 2006 I'm having some trouble inplementing that into my code. I'm very new to php.This is what I tried so far, however I'm not getting how to get it to all work together.[code]$results=mysql_query("SELECT * FROM $table WHERE Username='$Username'");while ($row=mysql_fetch_array($results)) {$pattern = "value='".$row['mail']."";str_replace($pattern, $pattern . ' checked=checked', $str); echo "<form action='index.php' method='post'>"; echo "Yes<input type='radio' name='mail' value='1'> No<input type='radio' name='mail' value='0'>"; echo "Yes<input type='radio' name='new' value='1'> No<input type='radio' name='new' value='0'>"; echo "<input type='hidden' name='update' value='update'>"; echo "<button type='submit'>Update</button>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/#findComment-39377 Share on other sites More sharing options...
poirot Posted May 28, 2006 Share Posted May 28, 2006 instead of echoing all that, put that into a string:[code]$results=mysql_query("SELECT * FROM $table WHERE Username='$Username'");while ($row=mysql_fetch_array($results)) { $str = "<form action='index.php' method='post'>"; $str .= "Yes<input type='radio' name='mail' value='1'> No<input type='radio' name='mail' value='0'>"; $str .= "Yes<input type='radio' name='new' value='1'> No<input type='radio' name='new' value='0'>"; $str .= "<input type='hidden' name='update' value='update'>"; $str .= "<button type='submit'>Update</button>"; $pattern = "value='".$row['mail'].""; $str = str_replace($pattern, $pattern . ' checked="checked"', $str); echo $str;}[/code]For example, if $row['mail'] is 0, It will search for value='0' and replace it with value='0' checked="checked". Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/#findComment-39613 Share on other sites More sharing options...
helpmeplease2 Posted May 28, 2006 Author Share Posted May 28, 2006 I'm now understanding how your code works, but its not working. Also, do I need a seperate $pattern for 'snmail', as I have 2 sets of radio buttons. When I select the radios and then click update it does update the database but it still displays 4 empty radio buttons in the screen.The $pattern is just saying if $row['mail'] is 0 then it will check the one with value='0', but its not looking for a 1 to check the value='1'. Would I use an }else{ thing for that, and how?[code]$str = "<form action='index.php?c=options' method='post'><table cellspacing='0' cellpadding='1' border='0' align='center'>";$str .= "<tr><td width='150'><b>Receive Promotional Email:</b></td><td width='200'><input type='radio' name='mail' value='1'> Yes <input type='radio' name='mail' value='0'> No</td></tr>";$str .= "<tr><td width='150'><b>Receive Promotional Snail Mail:</b></td><td width='200'><input type='radio' name='snmail' value='1'> Yes <input type='radio' name='snmail' value='0'> No</td></tr>";$str .= "<tr><td colspan='2'><input type='hidden' name='update' value='update'> </td></tr>";$str .= "<tr><td width='150'> </td><td align='center'><button type='submit'>Update Account Options</button></td></table>"; $pattern = "value='".$row['mail'].""; $str = str_replace($pattern, $pattern . ' checked="checked"', $str); echo $str;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/#findComment-39636 Share on other sites More sharing options...
poirot Posted May 28, 2006 Share Posted May 28, 2006 Actually it will search for value='RETURNED_VALUE' no matter what is the returned value. It gets quite confusing when you have more than 1 radio group, in this case you can separate the strings (like $str for 1st group and $str2 for the other).Then use str_replace twice. Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/#findComment-39641 Share on other sites More sharing options...
helpmeplease2 Posted May 28, 2006 Author Share Posted May 28, 2006 I have added a $str2 for my second radio group but its still not working. When I go to the page none of the radios are selected.Heres my updated code:[code]$str = "<form action='index.php?c=options' method='post'><table cellspacing='0' cellpadding='1' border='0' align='center'>";$str .= "<tr><td width='150'><b>Receive Promotional Email:</b></td><td width='200'><input type='radio' name='mail' value='1'> Yes <input type='radio' name='mail' value='0'> No</td></tr>";$str2 = "<tr><td width='150'><b>Receive Promotional Snail Mail:</b></td><td width='200'><input type='radio' name='snmail' value='1'> Yes <input type='radio' name='snmail' value='0'> No</td></tr>";$str2 .= "<tr><td colspan='2'><input type='hidden' name='update' value='update'> </td></tr>";$str2 .= "<tr><td width='150'> </td><td align='center'><button type='submit'>Update Account Options</button></td></table>"; $pattern = "value='".$row['mail'].""; $pattern2 = "value='".$row['snmail'].""; $str = str_replace($pattern, $pattern . ' checked="checked"', $str); $str2 = str_replace($pattern2, $pattern2 . ' checked="checked"', $str2); echo $str; echo $str2;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10558-grab-selected-radio-button-from-database/#findComment-39647 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.