Solved

How to set value to dropdown list

Posted on 2009-05-16
3
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

737 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