Jump to content


Photo

Grab selected radio button from database


  • Please log in to reply
6 replies to this topic

#1 helpmeplease2

helpmeplease2
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts

Posted 27 May 2006 - 04:20 AM

Say I have this....

Yes<input type='radio' name='email' value='1'> No<input type='radio' name='email' value='0'><

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?

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 27 May 2006 - 04:23 AM

There is no quick method, but you can use something like...

$pattern = "value='$val'";
str_replace($pattern, $pattern . ' checked=checked', $str);
$val is the value retrieved from MySQL, $str the string containing HTML cod.e
~ D Kuang

#3 helpmeplease2

helpmeplease2
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts

Posted 27 May 2006 - 04:40 AM

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.

$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>";
}


#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 28 May 2006 - 03:12 AM

instead of echoing all that, put that into a string:

$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;

}

For example, if $row['mail'] is 0, It will search for value='0' and replace it with value='0' checked="checked".
~ D Kuang

#5 helpmeplease2

helpmeplease2
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts

Posted 28 May 2006 - 04:32 AM

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?

$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;


#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 28 May 2006 - 06:02 AM

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.
~ D Kuang

#7 helpmeplease2

helpmeplease2
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts

Posted 28 May 2006 - 06:54 AM

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:
$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;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users