Solved

How to set value to dropdown list

Posted on 2009-05-16
3
560 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now