开发者

Populate radio buttons with values from database

two Qs in one day...

I'm trying to get the right radio button checked when I pull data from a mysql database to display in an edit form. Have tried all kinds of code but get the same result each time: the last value is always the one checked. Mystified.

My code is somewhat complicated by being inside a long html string, hence the construction for reading the php variables. I have this code at the top of t开发者_运维问答he page:

$row = mysql_fetch_assoc($result, MYSQL_ASSOC);
 if ($row)
 {
 print("<p>$se_id is in the database. You can edit this record.</p>");

 //get variables to set radio buttons in $form
 $type = $row['se_source'];
$checked[$type] = "checked";

etc... and this code inside the html string:

<li>
    <label for="se_source">Contributed by:</label>
          echo ' . $type . '
    <input id="se_source" type="radio" id = "radio" name="se_source"  value="CNFP" checked = ' . $checked["CNFP"] . '>CNFP</input>
    <input id="se_source" type="radio" id = "radio" name="se_source" value="User" checked = ' . $checked["User"] . '>User</input>

The echo statement is for my own sanity -- I know it's not really an echo here. But even when it says "echo CNFP" it is always the "User" button that is checked. So I know it is getting the correct value for that variable, but for some reason it defaults to the last value in the list. I've tried it with "===" as well. No joy. Any enlightenment appreciated, thanks so much for taking a look.


AFAIK, the value of the checked attribute isn't important, only the presence of it is sufficient to mark the checkbox as checked. (HTML5 standard confirms this)

Try something like this:

 <input id="se_source" type="radio" id = "radio" name="se_source"  value="CNFP"'.(($checked["CNFP"]) ? 'checked="checked"': '').'>CNFP</input>


The reason that you're always getting checked on the last one is because you're setting each radio button to checked consecutively.

When you pass a set of radio buttons with the same name to a browser, the default behavior is that the last button set as checked will be the one that shows up as checked. So, if you pass five radio buttons and they are all set as checked, the radio set cannot have more than one checked option.

When a browser encounters a second radio set option that is also marked as "checked," it simply removes "checked" from the first one.

Your php code is accomplishing this in this line:

$checked[$type] = "checked";

The problem with this is that you're creating an array that will show up with:

se_source => checked

So that means checked is the row value for every member in your array.

I think you should rethink your table structure and process. Instead of creating a column for the sake of making something checked, the following is what I use.

Basically, your problem is that you need:

  1. The value in the database to match

  2. The value of the radio button itself

Which will allow you to:

Compare the database value (DBV) with the radio button value (RBV) and set it as checked if the comparison comes back as true.

The variables below are:

$ar_rvbs = the array of radioset values (strings or booleans, usually) you are going to loop through and check against the stored DBV.

$value = the value of each item in the radioset value array

$attrval = the stored value for radio button. it doesn't necessarily have to be in a database. you could use the post method to pass it from one page to the next.

$checked = if the DBV matches the RBV when the loop goes through, this will be set to the string "checked" otherwise it is just an empty string.

function makeRadioSet($ar_rvbs,$attrval=[DBV] /*A*/
  {

  foreach ($ar_rvbs as $value)
     {
      $checked = '';                          /*B*/
      if ($attrval==$value)                   /*C*/
      $checked = "checked";                   /*D*/
      echo '<input type="radio" name = "fieldname" value = "'.$value.'" '.$checked.'>';
     }
  }

/A/ Pass the list of RBVs as an array and the DBV as a variable

/B/ Set checked to an empty string because that will be the default for all the radio buttons in the set except the one that matches the DBV

/C/ Compare the DBV to the current RBV being processed by the loop from the RBV array

/D/ If the comparison from step C returns true, make the checked string available for insertion into the input element

The echo takes care of generating each radio set option and making sure the one that matches the DBV has "checked" in the input element tag


You need to have the "checked" attribute ONLY on the selected radio button. So you need to modify your php code to echo in the correct format.


i think the bellow code will work well as you want. you must add a field to check whether it is checked.

while($result = mysql_fetch_array($queryRes)){
if(chack the radio button chacked field){
   echo "<input type="radio" checked="checked" value="value" name="same" />";
}
echo "<input type="radio" value="value" name="same" />";

}


Check this out! This could be what you are looking for :)

Generate checkbox based on entries on a MySQL table

$sql = "SELECT name FROM students";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
    echo "<input type='checkbox' name='students[]' value='".$row['name']."'>"
        .$row['name'];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜