Link to home
Start Free TrialLog in
Avatar of NoraWil
NoraWilFlag for Belgium

asked on

How to set value to dropdown list

Hi experts,

I'm new at php. I'm trying to create form that uses some dropdown menu/lists to let the user choose between a handful options. There are also quite a few text fields on the form.
Everything works perfect as long as the user fills in all the fields en lists. When the users clicks on 'submit' without filling in all text boxes en lists, I want to show exactly the same form as it was before pressing the 'submit' button.
This works perfect for all my text boxes, but all lists are reset.

I have collected the values of all contents in the dropdown lists with
$rubriek        =   $_POST["rubriek"];
$rubriek2        =  $_POST["rubriek2"];

When a submit failed, echo $rubriek; shows the correct choice of the user, but the dropdown list shows the first option again.
How can I set the value of the dropdown list to $rubriek??

I tried things like
form.rubriek.text = $rubriek;
of
form.rubriek.value=$rubriek;
but that doesn't do anything.

I can do the trick in Javascript, but that is not what I want.
Avatar of gamebits
gamebits
Flag of Canada image

Some code would help, how do you show the page again are you redirecting to the page? How do you keep track of the value submitted are you using session?

Usually you will have something like this

<select name="whatever">
     if(isset($whatever)){echo "<option value=\"$whatever\">$whatever</option>
    then the regular list of options
</select>
ASKER CERTIFIED SOLUTION
Avatar of pedro_sland
pedro_sland
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NoraWil

ASKER

Thank you, this does the job.