Solved

Using Onchange behaviour of a dropdown list to submit a form

Posted on 2006-06-14
4
498 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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