Solved

Using Onchange behaviour of a dropdown list to submit a form

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now