Solved

How to set value to dropdown list

Posted on 2009-05-16
3
570 Views
Last Modified: 2013-12-13
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.
0
Comment
Question by:NoraWil
3 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24402914
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>
0
 
LVL 2

Accepted Solution

by:
pedro_sland earned 500 total points
ID: 24402919
Use something like this:
<select name="rubrick">
  <option value="1" <?php if($_POST['rubrick'] && $_POST['rubrick'] == 1) echo 'selected'; ?>>One</option>
  <option value="2" <?php if($_POST['rubrick'] && $_POST['rubrick'] == 2) echo 'selected'; ?>>Two</option>
  <option value="3" <?php if($_POST['rubrick'] && $_POST['rubrick'] == 3) echo 'selected'; ?>>Three</option>
</select>

Open in new window

0
 

Author Closing Comment

by:NoraWil
ID: 31582237
Thank you, this does the job.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 62
how to access a remote mysql database with xampp 3 24
if (is_singular not working 5 19
show child records separated by commas 12 10
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question