Solved

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

Posted on 2011-09-07
5
179 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
  • 3
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 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 51

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

910 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

20 Experts available now in Live!

Get 1:1 Help Now