Reputation: 64
I have the following code and it insert the necessary data to the database perfectly. but the problem is that when i want to implement select="select".. so that if the customer return he will see what he choose from the dropdown menu...how can i do that, any idea...
<pre>
<?php
//array generated from mysql to feed first dropdown menu.
$newOptions = array();
foreach ($input as $option) {
$wclID = $option['desc'];
$nameF = $option['fame'];
$nameL = $option['lname'];
$id = $option['ID'];
$newOptions[$wclID][$id] = $nameL." ".$nameF;
}
//second array for second dropdown menu
$array = array('ent','res');
?>
<div class="control-group">
<div class="controls">
<form id="checkEnt" name="che" method="post" action="com.php?ID=<?=$ID?>">
<input type="hidden" value="che" name="ent">
<table class="table table-striped span10">
<tbody>
<? foreach ($newOptions as $wclID => $list) { ?>
<tr><td width="5%">
<h5><?=$wclID?> AA</h5>
</td>
<td width="10%">
<label class="control-label" for="inputWei"><?=_('Boy')?></label>
<select class="input-xlarge" id="input" name="drop[0][]">
<option value=""><?=_('[select]')?></option>
<?php
foreach ($list as $key => $value) {
?><option value="<?=$key?>"><?=$value?></option>
<?php } ?>
</select>
</td>
<td width="10%">
<label class="control-label" for="inputWei"><?=_('Res')?></label>
<select class="input-xlarge" id="drop" name="drop[1][]">
<option value=""><?=_('[select]')?></option>
<?php
foreach ($array as $key => $value) {
?><option value="<?=$value?>"><?=$value?></option>
<?php } ?>
</select>
</td>
</tr>
<?
}
?>
<tr><td colspan="3">
<div class="modal-footer">
<button type=reset class="btn btn-danger"><?=_('Reset')?></button>
<button class="btn btn-primary" ID="btnSave"><?=_('Save')?></button>
</div>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</pre>
Upvotes: 0
Views: 194
Reputation: 3602
I recently did something similar to this. This is for data that is coming back from the database correct? If so then I would implement something similar to this.
On the option tags I would try an if statement to see what is in the database and then if it is call the selected=selected
<option value="Something" <? if(in_array('Something',$nameofArray)) echo 'selected="selected"';?>>Something</option>
or in your case you might want to try
<?foreach ($list as $key => $value) {?>
<option value="$value" <? if($value) echo 'selected="selected"';?>>$value</option>
<?}?>
Upvotes: 2
Reputation: 861
Use the "selected" attribute of the option tag:
http://www.w3schools.com/tags/att_option_selected.asp
Upvotes: -1