select the correct value in my selection form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

select the correct value in my selection form

Post by aneuryzma »

How can I select the correct value in my "Select form field", once I know the value from php/mysql?

Code: Select all

$_SESSION['job'] = "FX";
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>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: select the correct value in my selection form

Post 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>.
(#10850)
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: select the correct value in my selection form

Post 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.
Last edited by Benjamin on Mon May 11, 2009 1:50 am, edited 1 time in total.
Reason: Changed code type from text to php.
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: select the correct value in my selection form

Post 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>
 
Last edited by Benjamin on Mon May 11, 2009 2:43 am, edited 1 time in total.
Reason: Changed code type from text to html.
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

Re: select the correct value in my selection form

Post by aneuryzma »

I've solved!

just removing selected="false"

thanks anyway
Post Reply