Jump to content

Grab selected radio button from database


helpmeplease2

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?
Link to comment
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
Link to comment
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]
Link to comment
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".
Link to comment
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]
Link to comment
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.
Link to comment
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]
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.