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

x
?
Solved

How to show (or change) different htlm form fields  based on a form selection

Posted on 2011-09-07
5
Medium Priority
?
187 Views
Last Modified: 2012-05-12
I would like to be able to change form fields based on a form field selection.  For example I have this form to start:

<form name="myform" action="index.php" method="POST">                   
        <select name="select">
                    <option value="1">Select One</option>
                    <option value="2">Select Two</option>
        </select>
        <select name="type">
                    <option value="1">Type One</option>
                    <option value="2">Type Two</option>
        </select>

What I would like is to then show the next field (If_type_ one, or if_type_two) based on my selection of field "type" above.  

        <select name="if_type_one">
                    <option value="1">Type One Something</option>
                    <option value="2">Type One Something Else</option>
        </select>

        <select name="if_type_two">
                    <option value="1">Type Two Something</option>
                    <option value="2">Type Two Something Else</option>
        </select>

Thanks......


       
0
Comment
Question by:dmalovich
[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
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36498684
Putting the selects inside div :
<div id="type1">
    <select name="if_type_one">
        <option value="1">Type One Something</option>
        <option value="2">Type One Something Else</option>
    </select>
</div>
  
<div id="type2">
<select name="if_type_two">
    <option value="1">Type Two Something</option>
    <option value="2">Type Two Something Else</option>
</select>
</div>

Open in new window

You may use :
function hide(who) {
    document.getElementById(who).style.display = "none";
}

function show(who) {
    document.getElementById(who).style.display = "block";
}

function updateType(val) {
    switch(val) {
            case "1" :show("type1");hide("type2");break;
            case "2" :show("type2");hide("type1");break;
    }
}

Open in new window


Test page : http://jsfiddle.net/427VR/
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36498698
you may use (without the div around the selects but setting their ID attributes):

<select name="if_type_one" id="type1">


<select name="if_type_two" id="type2">
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36498700
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 36498894
here is what you need I guess
<script>

function ShowOptions(n){
  document.getElementById("if_type_one").style.display=(n==1)?"":"none";
  document.getElementById("if_type_two").style.display=(n==2)?"":"none";
}
</script>
<form name="myform" action="index.php" method="POST">                    
        <select name="select">
                    <option value="1">Select One</option>
                    <option value="2">Select Two</option>
        </select>

        <select name="type" onChange="ShowOptions(this.value)">
                    <option value="1">Type One</option>
                    <option value="2">Type Two</option>
        </select>

        <select name="if_type_one" id="if_type_one" style="display:">
                    <option value="1">Type One Something</option>
                    <option value="2">Type One Something Else</option>
        </select>

        <select name="if_type_two" id="if_type_two" style="display:none">
                    <option value="1">Type Two Something</option>
                    <option value="2">Type Two Something Else</option>
        </select>

</form>

Open in new window

0
 

Author Closing Comment

by:dmalovich
ID: 36499131
It worked.  Thank you.....
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

636 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