Solved

Using Onchange behaviour of a dropdown list to submit a form

Posted on 2006-06-14
4
499 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
[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
4 Comments
 
LVL 3

Accepted Solution

by:
spotx earned 250 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 250 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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.

635 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