Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Onchange behaviour of a dropdown list to submit a form

Posted on 2006-06-14
4
Medium Priority
?
503 Views
Last Modified: 2013-12-12
I want to amend my page so that depending on the value selected in a drop down list the page will display fields relevant to the selected value only. I am assuming the best way to do this is to submit the form onchange of the dropdown, and display the fields I want depending on what value was selected. My question is how to I get the form to submit ? I am using PHP/Javascript and a MySQL database.
0
Comment
Question by:fergal_lawlor
2 Comments
 
LVL 3

Accepted Solution

by:
spotx earned 1000 total points
ID: 16909497
This is pretty straight forward
The select tag looks something like this
<SELECT id='sel1' onchange='this.form.submit();' >
the value selected will be in the $_POST['sel1'] variable

you could make your javascript a bit cooler by hidding and showing the relevant fields without the page select

A simple but clunky way to do this would be

<script>
function show_fields(sel1_val){
if(sel1_val == '2and3'){
//blank the hidden fields so they wont get submitted
document.getElementById('field_1').value ='';
//show the right fields
document.getElementById('field_1').style.visiblity ='hidden';
document.getElementById('field_2').style.visiblity ='visible';
document.getElementById('field_3').style.visiblity ='visible';
}else{
document.getElementById('field_1').style.visiblity ='visible';
//blank the hidden fields so they wont get submitted
document.getElementById('field_2').value ='';
document.getElementById('field_3').value ='';
//show the right fields
document.getElementById('field_2').style.visiblity ='hidden';
document.getElementById('field_3').style.visiblity ='hidden';
}

}

</script>

the the select tag looks like this

<SELECT id='sel1' onchange='show_fields(this.options[this.selectedIndex].value;);' >
There are lots of way to do this but thats a couple of suggestions
0
 
LVL 4

Assisted Solution

by:anilande
anilande earned 1000 total points
ID: 16909888
<form name='frm' >
<select onchange="document.getElementByID('frm').submit();">
</select>
</form>

i think for showing fields onchange event, submitting the form is not a good idea.
us a javascript way. means hidding and showing divs as suggested by spotx

 or even better way is to use ajax to do this.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

782 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