Page 1 of 1
select the correct value in my selection form
Posted: Mon May 11, 2009 12:53 am
by aneuryzma
How can I select the correct value in my "Select form field", once I know the value from php/mysql?
Now I want to update the form to display the selected value...
Code: Select all
<select multiple="true" name="field"> <option value="FL" selected="true">Flash</option> <option value="FX">Flex</option> <option value="AJ">Ajax</option> <option value="PH">Php</option> <option value="HT">Html</option> <option value="JV">Java</option> <option value="CC">C++</option></select>
Re: select the correct value in my selection form
Posted: Mon May 11, 2009 1:09 am
by Christopher
Put the option values in an array. Loop through the values to generate each <option>. Each iteration check if the option value is the same as the current value. If they are the same then add selected="selected" to that <option>.
Re: select the correct value in my selection form
Posted: Mon May 11, 2009 1:49 am
by Griven
Like Arborint said, you first have to put all of your job information into an array. You can do this by declaring it somewhere else in your code, or by pulling it from a database.
The following example assumes that your job information is being taken from an array called "jobinformation" pulled from MySQL :
Code: Select all
while($row = mysql_fetch_array($jobinformation)){
echo "<option value="', $row['job'] ,'";
if($row['job'] == $_SESSION['job']){
echo ' selected="selected"';
}
echo '>', $row['job_fullname'] ,'</option>';
}
Since you've set your selection area to "multiple='true'", you can also make the $_SESSION['job'] variable an array, and change the IF statement to read something like:
Code: Select all
if(in_array($row['job'], $_SESSION['job']))
This will enable the WHILE loop above to print out multiple preselected options.
Re: select the correct value in my selection form
Posted: Mon May 11, 2009 2:06 am
by aneuryzma
ok thanks, I've done and this is the resulting html code.
Still I don't understand why *all* the items of my selection menu are selected, instead of only the selected one.
Code: Select all
<select>
<option selected="false" value="FL">Flash</option>
<option selected="true" value="FX">Flex</option>
<option selected="false" value="AJ">Ajax</option>
<option selected="false" value="PH">Php</option>
<option selected="false" value="HT">Html</option>
<option selected="false" value="JV">Java</option>
<option selected="false" value="CC">C++</option>
</select>
Re: select the correct value in my selection form
Posted: Mon May 11, 2009 2:09 am
by aneuryzma
I've solved!
just removing selected="false"
thanks anyway