Solved

Using Onchange behaviour of a dropdown list to submit a form

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Inserting results of While loop to sql server 5 38
How to get this library to work load? 8 38
CURL sending XML without spaces PHP 5 33
Put POST values into cookies. 14 34
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

749 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