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
)
)