I have an form with an multiple select in the while loop:
while ($row_i = mysql_fetch_array($res_i))
{
$i++;
// maak select name
$name_bewerking_id = 'bewerking_id'.$i;
?>
<tr valign="top">
<td>
<select name="<?php echo $name_bewerking_id ?>[]" multiple="multiple" size="2">
<?php
$sql = "SELECT id, bewerking FROM bewerkingen ORDER BY bewerking ASC";
$res = mysql_query($sql,$con);
while ($row = mysql_fetch_assoc($res))
{ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['bewerking']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php
}
When the form is send:
$bewerking_id[$i] = array();
$bewerking_id[$i] = $_POST['name_bewerking_id'][$i];
if(isset($bewerking_id_temp[$i]))
{
foreach($bewerking_id_temp[$i] as $temp[$i])
{
array_push($bewerking_id[$i], $temp[$i]);
}
}
Returning to the form:
for ($i = 0; $i <= $aantal_regels_corr; $i++)
{
// maak select name
$name_bewerking_id = 'bewerking_id'.$i;
?>
<tr valign="top">
<td>
<select name="<?php echo $name_bewerking_id ?>[]" multiple="multiple" size="2">
<?php
$sql = "SELECT id, bewerking FROM bewerkingen ORDER BY bewerking ASC";
$res = mysql_query($sql,$con);
while ($row = mysql_fetch_assoc($res))
{ ?>
<option <?php if(isset($bewerking_id[$i]) && in_array($row['id'], $bewerking_id[$i])){ echo 'selected="selected"'; } ?> value="<?php echo $row['id']; ?>"><?php echo $row['bewerking']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php
}
When returning to the form (when one of the other fields is not filled in) the chosen option(s) are lost and not selected again.
Where did I messed up?
You read POST data with this identifier
'name_bewerking_id'
but the select name is given by
<?php echo $name_bewerking_id ?>
with$name_bewerking_id = 'bewerking_id'.$i