Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to set value to dropdown list

Posted on 2009-05-16
3
Medium Priority
?
593 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 2000 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

Industry Leaders: 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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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 …
Suggested Courses

610 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