Jump to content

Archived

This topic is now archived and is closed to further replies.

helpmeplease2

Grab selected radio button from database

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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 &nbsp;&nbsp;&nbsp;&nbsp;<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 &nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' name='snmail' value='0'> No</td></tr>";
$str .= "<tr><td colspan='2'><input type='hidden' name='update' value='update'>&nbsp;</td></tr>";
$str .= "<tr><td width='150'>&nbsp;</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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 &nbsp;&nbsp;&nbsp;&nbsp;<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 &nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' name='snmail' value='0'> No</td></tr>";
$str2 .= "<tr><td colspan='2'><input type='hidden' name='update' value='update'>&nbsp;</td></tr>";
$str2 .= "<tr><td width='150'>&nbsp;</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]

Share this post


Link to post
Share on other sites

×

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.