Pre-populating radio buttons, checkboxes and select menues

Hi,
I have a form which I use to input data. The form uses radio buttons, checkboxes and a select menu.
I want to use the same form to edit the data. So I need to repopulate the radio button, checkbox and select menu. But I can’t figure out how to do this.

Here is part of the html form,

<input type="radio" name="attend[<?php echo $x; ?>]" value="absence"> 
                        </td> 
                        <td>
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="late"> 
                        </td>
                        <td>
                           <input type="checkbox" name="equip[<?php echo $x; ?>]" value="1"> 
                        </td>
                        <td>
                            <select name="effort[<?php echo $x; ?>]">
                            <option value="0"></option>
                            <?php
                             foreach(range(1,5) as $ef):
                             echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
                             endforeach;
                             ?>
                            </select>
                        </td>
                        <td>
                            <textarea name="comment[<?php echo $x; ?>]" rows="1" cols="40"><?php htmlout($comment); ?></textarea>
                        </td>

The data I am trying to get in is: attendance and late in the radio buttons, equipment in the checkbox and effort in the select menu. The values for each are in this array called $row,

Array
(
    [id] => 98
    [0] => 98
    [absence] => 0
    [1] => 0
    [late] => 0
    [2] => 0
    [disrupt] => 
    [3] => 
    [equip] => 1
    [4] => 1
    [lazy] => 
    [5] => 
    [phone] => 
    [6] => 
    [rude] => 
    [7] => 
    [effort] => 5
    [8] => 5
    [comment] => Likes big explosion
    [9] => Likes big explosion
    [date] => 2015-02-21
    [10] => 2015-02-21
    [time] => 10:06:36
    [11] => 10:06:36
    [userid] => 49
    [12] => 49
    [courseid] => 33
    [13] => 33
)

I am not sure why the values in that array above are doubled. But it is not really an issue right now. In the index.php file I set those values in the array to the variables.

 try
  {
    $sql = 'SELECT * FROM notes WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching notes for editing.';
    include 'error.html.php';
    exit();
  }
  $row = $s->fetch();

  $id = $row['id'];


 $courseid = $row['courseid'];
  $learnerid = $row['userid'];
  $attend = $row['attend'];
  $equip = $row['equip'];
  $effort = $row['effort'];
  $comment = $row['comment'];

I can pre-populate comments but not the other forms.

Any help would be appreciated,
Thanks,
Shane

PS I have just remembered how to fix the issue with the double values in the array. In the index,php file i use,

  //$row = $s->fetch();
  $row = $s->fetchAll(PDO::FETCH_ASSOC);

Which gives a much neater array for $row,

Array
(
    [0] => Array
        (
            [id] => 98
            [absence] => 0
            [late] => 0
            [disrupt] => 
            [equip] => 1
            [lazy] => 
            [phone] => 
            [rude] => 
            [effort] => 5
            [comment] => Likes big explosion
            [date] => 2015-02-21
            [time] => 10:06:36
            [userid] => 49
            [courseid] => 33
        )

)

Just skimmed…

When you submit an HTML Form on a PHP page, your script should process the entry, right?

As such, you should be storing things in a Variable, Array or directly into the Database.

Well, when you re-load the Form, just grab the data for a given field from the Variable, Array, or Database Field where you stored the user’s data.

And then echo things.


When a user completes my form, I store their Gender in the $gender variable. Then I write it to my database.

And then when the form is reloaded for editing, I grab the Gender from the database and populate the $gender variable and then echo it in the Form like this…

<!-- Gender -->
<label for="gender">Gender:</label>                        
<label class="inlineRadioLabel" for="gender_opt1">
<input id="gender_opt1" type="radio" name="gender" value="1" <?php echo (isset($gender) && $gender == "1") ? 'checked="checked"' : ''; ?>/>
Male
</label>

<label class="inlineRadioLabel" for="gender_opt2">
<input id="gender_opt2" type="radio" name="gender" value="2" <?php echo (isset($gender) && $gender == "2") ? 'checked="checked"' : ''; ?>/>
Female
</label>

HTH

Hi mikey_w,
Thanks this works great now. Is that you use the tenary operator.

<input type="radio" name="attend[<?php echo $x; ?>]" value="absence" <?php echo (isset($absence) && $absence == "1") ? 'checked="checked"' : ''; ?> > 
                        </td> 
                        <td>
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="late" <?php echo (isset($late) && $late == "1") ? 'checked="checked"' : ''; ?> > 
                        </td>
                        <td>
                           <input type="checkbox" name="equip[<?php echo $x; ?>]" value="1" <?php echo (isset($equip) && $equip == "1") ? 'checked="checked"' : ''; ?> > 
                        </td>
                        <?php echo $attend;?>
                        <td>
                            <select name="effort[<?php echo $x; ?>]">
                            <option value="0"></option>
                            <?php
                             foreach(range(1,5) as $ef):
                             echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
                             endforeach;
                             ?>
                            </select>
                        </td>
                        <td>
                            <textarea name="comment[<?php echo $x; ?>]" rows="1" cols="40"><?php htmlout($comment); ?></textarea>
                        </td>

Now how do pre-populate the select menu/
Thanks,
Shane

Ok I see that I can just do the same thing for the select menu,

<td>
                            <select name="effort[<?php echo $x; ?>]">
                            <option value="0"><?php echo (isset($effort)) ? $effort : ''; ?> </option>
                            <?php
                             foreach(range(1,5) as $ef):
                             echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
                             endforeach;
                             ?>
                            </select>
                        </td>

Thanks,
Shane

Glad to help.

Yes.

To learn how other controls work, I would encourage you to either look here on SitePoint, or go to W3Schools

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.