473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dependant dropdowns with PHP?

348 100+
I got a winner here..

I need a solution that will use a single html select and two input fields that depend on the value selected.

This dropdown will have 3 values. If a certain values is selected, I need two input boxes to appear. If either of the other two values are selected, then no additional input boxes should be added.

This sounds like a job for AJAX i'm sure but I haven't a clue about AJAX.

What I have now is a select tag with a js onchange event that fires when the user selects the value from the select box. With php, I test for the POST var and if it is the value where I need the additional input boxes, I echo them. This is all well and good until I need to submit the page.

In theory, I setup the page using the onchange and then submit the entire page after it has been completed. This is where I have my problem because to do it like this, I would need to nest form tags and I don't think this is correct.

Does someone have a code sample they could share with me or point me in the direction that I need to go?

Thanks,

Frank
Aug 13 '08 #1
18 1534
r035198x
13,262 8TB
Will both values be coming from the database? In which case AJAX would be the best approach.
Aug 13 '08 #2
fjm
348 100+
Will both values be coming from the database? In which case AJAX would be the best approach.
Hi r035198x,

No, actually, none of these will require db access to populate. I am populating the select box with hard coded values. There will be 3 option values (1,2, and 3).

When the user selects option #2 I need to show another two selects to the user. Just for clarification, when option #2 is selected it is only acting as a trigger so to speak to show the additional selects. (And also using the value for $_POST when the form is submitted.

In other words, if option #2 is NOT selected, I don't want to see the additional two selects. Of course, if it is, show them.

When the entire form is completed, the values from all of the selects, whether a single select or all three need to be submitted.

I am not sure how to handle this....

Thanks for the help r035198x!

Frank
Aug 13 '08 #3
Perhabs this may be useful.
Instead of using the style display: block, none you can also use style visibility: visible, hidden.
I used display since it does not hold the space of the elements.

[HTML]<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Select 1:
<select name="select1" onchange="if (this.value == 2) { document.getElementById('other_two').style.display ='block'; } else { document.getElementById('other_two').style.display =none; } ">
<option value="">select</option>
<option value="1">option - 1</option>
<option value="2">option - 2</option>
<option value="3">option - 3</option>
</select>
<div id="other_two" style="display: none">
Select 2:
<select name="select2">
<option value="">select</option>
<option value="1">option - 1</option>
<option value="2">option - 2</option>
<option value="3">option - 3</option>
</select>
<br>
Select 3:
<select name="select3">
<option value="">select</option>
<option value="1">option - 1</option>
<option value="2">option - 2</option>
<option value="3">option - 3</option>
</select>
</div><br>
<input type="submit" name="submit" value="submit">
</form>[/HTML]
Aug 13 '08 #4
r035198x
13,262 8TB
You don't need to post to the server to get that functionality. All you need is Javascript.
Aug 13 '08 #5
fjm
348 100+
r035198x,

Thanks again for the help. Javascript was never a passion of mine so I am not really sure what I should use in this case. Is there a term I can search for?

Samatair,

Thanks for the snippet. It seems to work great. The only issue that I can forsee would be if the user made a mistake and entered the wrong value option, the two additional selects would render and will not go away which would force a post value on both of the selects.

It certainly is cool to know that about css. Thank you! If all else fails and I can't find a js solution, I will definetley be using yours. :)

Frank
Aug 13 '08 #6
I have missed single quotes around the value 'none'.
If you add them the script should work fine displaying the select boxes when option 2 is selected and remove them from viewing when other options are selected.
Try using them. If it does not satisfy your needs you can always google out. Best of Luck.


[HTML]<?php
if (isset($_POST['submit'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?><form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Select 1:
<select name="select1" onchange="if (this.value == 2) { document.getElementById('other_two').style.display ='block'; } else { document.getElementById('other_two').style.display ='none'; } ">
<option value="">select</option>
<option value="1">option - 1</option>
<option value="2">option - 2</option>
<option value="3">option - 3</option>
</select>
<div id="other_two" style="display: none">
Select 2:
<select name="select2">
<option value="">select</option>
<option value="1">option - 1</option>
<option value="2">option - 2</option>
<option value="3">option - 3</option>
</select>
<br>
Select 3:
<select name="select3">
<option value="">select</option>
<option value="1">option - 1</option>
<option value="2">option - 2</option>
<option value="3">option - 3</option>
</select>
</div><br>
<input type="submit" name="submit" value="submit">
</form>
[/HTML]
Aug 14 '08 #7
pbmods
5,821 Expert 4TB
To get the selected value of a <select>, you have to use this.options[this.selectedIndex].value instead of this.value.
Aug 14 '08 #8
fjm
348 100+
Hey Pbmods,

Do you mean in my onchange event? Sorry. I am totally lost when it comes to js.

Say, how hard is js to learn anyway? With the advent of ajax, I think it may be an advantage to know.

arggg.. another learning curve to overcome.... :(
Aug 14 '08 #9
r035198x
13,262 8TB
Hey Pbmods,

Do you mean in my onchange event? Sorry. I am totally lost when it comes to js.

Say, how hard is js to learn anyway? With the advent of ajax, I think it may be an advantage to know.

arggg.. another learning curve to overcome.... :(
He's talking about line 9 in the code above.
Aug 14 '08 #10
To get the selected value of a <select>, you have to use this.options[this.selectedIndex].value instead of this.value.

Thank you very much for getting me right with "this.options".
But it worked for me. I think your way should be right.
Pls can you let me know the difference between the two.
If only I am not diverting from fjm's question.
Aug 14 '08 #11
acoder
16,027 Expert Mod 8TB
To get the selected value of a <select>, you have to use this.options[this.selectedIndex].value instead of this.value.
Hmm, why not this.value?

By the way, I think this thread should be moved to the JavaScript forum.
Aug 14 '08 #12
fjm
348 100+
He's talking about line 9 in the code above.
Thanks r035198x for pointing that out. At least I can follow along now.

I really need to pick up a book on javascript.
Aug 14 '08 #13
fjm
348 100+
Thank you very much for getting me right with "this.options".
But it worked for me. I think your way should be right.
Pls can you let me know the difference between the two.
If only I am not diverting from fjm's question.
Thanks Samatair for the code and the help. Your solution worked out great!

The this.options is above me right now although I will follow the w3 link provided by acoder to read up on it.

Whatever Samatair has in his code seems to work great.

:)
Aug 14 '08 #14
pbmods
5,821 Expert 4TB
Hmm, why not this.value?
Huh. When'd they do that?

And more pertinently to my situation (unfortunately), does IE support it?
Aug 14 '08 #15
acoder
16,027 Expert Mod 8TB
...although I will follow the w3 link provided by acoder to read up on it.
I wouldn't advise it if you're new to JavaScript. It's not an easy read.

Try some of the tutorial links in this thread in the JavaScript forum.
Aug 15 '08 #16
acoder
16,027 Expert Mod 8TB
Huh. When'd they do that?

And more pertinently to my situation (unfortunately), does IE support it?
Since DOM Level 1, it seems, so probably around 1998.

Yes, IE does support it, but you have to ensure that the options have values. Here's something I prepared earlier :)
Aug 15 '08 #17
fjm
348 100+
Thanks Acoder. I will read the links you provided.

This may sound crazy, but I have always hated js and thought it was some kinda cheap wanna be language, but after getting that solution from Samatair, I am starting to change my mind. That was a perfect solution and fit very well for what I needed. Funny, but I couldn't do that with php. I know that php is server side only but one would like to think that php is possible of doing anything. :)
Aug 15 '08 #18
acoder
16,027 Expert Mod 8TB
This may sound crazy, but I have always hated js and thought it was some kinda cheap wanna be language
JavaScript did have a bad reputation and is still to some extent the world's most misunderstood programming language. Deep down, it's actually a very rich and powerful language. If you use it correctly, it's very useful, but it can also be misused.
Aug 15 '08 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Crescionini Sascha | last post by:
Hello NG If I let appear a DIV over a HTML site with dropdowns, this dropdowns are still visible. Is there a way to push them to the back? All other form elements arent visible. greets...
0
by: Jeffrey | last post by:
Hi... I have several dropdowns located in a datagrid footer. Now say i modify the selection in dropdown1. I want to refresh the content of the other dropdowns with the result of a database...
3
by: Damon | last post by:
I am working on a site in which I would like to have two dropdowns that will allow a user to navigate through the administrative pages of the site. The first would allow the user to choose the...
1
by: Andy | last post by:
What I want to do is to populate multiple dropdowns when editing. Presumably... a) I should use a DataReader so that I can get each ResultSet for each dropdown control, and that should cut down...
2
by: bingomanatee | last post by:
I have developed what amounts to a fancy shopping cart wizard for a scientific instrument using VB.NET. We are having some disturbing phenomena relating to dropdown controls. On my system and...
0
by: nkparimi | last post by:
Hi, Here's what I'm trying: given an html table, to freeze the column headers and/or left column in IE. I understand that this is possible thru style sheets in IE, as suggested in the following:...
3
by: Simon Harvey | last post by:
Hi everyone, I keep getting a problem with dropdownlist controls. It sounds really stupid, but my app is screwed as long as this keeps happening. It seems to spontaneously happen and then I...
1
by: Stimp | last post by:
I have 3 dropdowns: country, county and district. I'm using ajax to dynamically populate the county and district dropdowns when country dropdown is changed (and similarly the district dropdown...
0
by: bogorman | last post by:
Am trying to add a "video" to a webpage which is based on a template containing a javascript menu. The site has hundreds of pages all based on this template and the menu works fine The page can be...
0
by: sbart | last post by:
I am programing in asp.net vb using AjaxToolkit cascadingDropDown. I do not have access to AjaxToolkit cascading dropdown event handlers. I have a series of dropdowns. The selection from one of...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.