Hide/show a text box based on drop down list in php

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
asmitacp
Forum Newbie
Posts: 16
Joined: Wed Mar 26, 2008 7:36 am

Hide/show a text box based on drop down list in php

Post by asmitacp »

Can anybody tell me the code to Hide/show a text box based when particular values in drop down list is selected in php.
or
the code to display another drop down when particular value is selected in drop down list in php
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Hide/show a text box based on drop down list in php

Post by mchaggis »

It depends on if you want the form to be submitted prior to the new drop down / portion of html to be displayed or if you want to do it in. The PHP side is easy as you would do something similar to:

Code: Select all

<?php if ($_REQUEST['nameofdropdown'] == 'somevalue') { ?>
<select name="blah">
...
</select>
<?php } ?>
However, I suspect that you are wanting to do it while the user is stil on that page, that case you will need to use javascript. I usually use hidden div's to do this in the following way:

Code: Select all

 
<script>
function hideShow( selectObj, DivId ) {
    if (selectObj.options[selectOdj.selectedIndex].value == 2) {
        document.getElementById(DivId).style.display='block';
    } else {
        document.getElementById(DivId).style.display='none';
    }
}
</script>
 
<select name="thisname" onchange="hideShow(this, 'nextDiv')">
<option value="1">First</option>
<option value="2">First</option>
<option value="3">First</option>
</select>
<div id="nextDiv" style="display: none">
 
Next block of HTML
 
</div>
 
Obviously the example is very basic, but once you get to grips with the above, you can do some really quite advanced stuff (including ajax), but the basic principle is always the same.

Hope that helps
asmitacp
Forum Newbie
Posts: 16
Joined: Wed Mar 26, 2008 7:36 am

Re: Hide/show a text box based on drop down list in php

Post by asmitacp »

I tried the code u given but it is not working at all.
asmitacp
Forum Newbie
Posts: 16
Joined: Wed Mar 26, 2008 7:36 am

Re: Hide/show a text box based on drop down list in php

Post by asmitacp »

<script>
function change_course(selectObj, DivId){
if (selectObj.options[selectOdj.selectedIndex].value ==4) {
document.getElementById(DivId).style.display='show';
} else {
document.getElementById(DivId).style.display='hide';
}
}
</script>

//-------------html code--------------
<select name="ODCourse" class="form_text" id="ODCourse" onChange="change_course(this,'disp_none')">
<option value="" selected>Select</option>
<option value="1">Basic Postal </option>
<option value="2">Children Postal </option>
<option value="3">Basic Residential</option>
<option value="4">Basic Non-residential</option>
</select>

<div id="disp_none" style="display:none">
<tr class="hide">
<td colspan="4" align="left" valign="top" class="bodytext6">
<span class="bodytext666">Place:&nbsp;&nbsp;
<input name="r_place2" type="text" id="r_place6">
</span></td>
</tr>
</div>

this is my code but it is not hiding the text box .
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Hide/show a text box based on drop down list in php

Post by mchaggis »

Unfortunately you can't put the divs round table rows. It is an annoyance that I have found. If you make the table columns a fixed width, then the method I use is to break out of the table:

Code: Select all

 
<script>
function change_course(selectObj, DivId){
if (selectObj.options[selectOdj.selectedIndex].value ==4) {
document.getElementById(DivId).style.display='show';
} else {
document.getElementById(DivId).style.display='hide';
}
}
</script>
 
//-------------html code--------------
<select name="ODCourse" class="form_text" id="ODCourse" onChange="change_course(this,'disp_none')">
<option value="" selected>Select</option>
<option value="1">Basic Postal </option>
<option value="2">Children Postal </option>
<option value="3">Basic Residential</option>
<option value="4">Basic Non-residential</option>
</select>
 
<div id="disp_none" style="display:none">
<table>
<tr class="hide">
<td colspan="4" align="left" valign="top" class="bodytext6">
<span class="bodytext666">Place:&nbsp;&nbsp;
<input name="r_place2" type="text" id="r_place6">
</span></td>
</tr>
</table>
</div>
 
 
Alternatively, but the div inside the td:

Code: Select all

 
<tr class="hide">
<td colspan="4" align="left" valign="top" class="bodytext6">
<div id="disp_none" style="display:none">
<span class="bodytext666">Place:&nbsp;&nbsp;
<input name="r_place2" type="text" id="r_place6">
</span>
</div>
</td>
</tr>
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Hide/show a text box based on drop down list in php

Post by pickle »

So many things to say:

PHP is a server-side language, not client-side. User interaction with a page is handled with Javascript. Therefore...
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
I'm moving this thread there.


Please use [ code=javascript ][ /code ] tags as it's MUCH easier to read.
Last edited by pickle on Mon Mar 31, 2008 9:58 am, edited 1 time in total.
Reason: Removed errant [/list]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Hide/show a text box based on drop down list in php

Post by mchaggis »

pickle wrote:So many things to say:

PHP is a server-side language, not client-side. User interaction with a page is handled with Javascript. Therefore...
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
I'm moving this thread there.


Please use [ code=javascript ][ /code ] tags as it's MUCH easier to read.[/list]
That's fair enough pickle, but quoting the "Rules" section is a bit harsh as if you read asmitacp's first post, asmitacp clearly thought that this could be done within PHP, so even if asmitacp had read the rules that you refer to, then as asmitacp thought the answer lay within PHP this thread would have still been posted hear! It was only once I pointed out that it had to be done via javascript did this thread move away from a PHP question, so by all means move the thread to an appropriate place, but cutting out the authoritarian attitude will go a long way. I also suspect that you have just copied and pasted your message from a list of stock reasons for moving threads. :dubious:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Hide/show a text box based on drop down list in php

Post by pickle »

It's actually a Greasemonkey script that has all our board rules & that was built for exactly this reason.

Perhaps it was a bit harsh, but I deal with mis-posted threads all day. Most of them are mis-posted simply because the original poster was too lazy to read the rules & look through ALL our forums before posting. It's difficult to figure out which threads are legitimately mis-posted & which are due to laziness.

Thank you for your comments.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Hide/show a text box based on drop down list in php

Post by mchaggis »

No worries pickle, I;ve done my fair share of forum/chat room moderation over the every increasing years and know what it's like. Looks like the greasemonkey script is broke cos it put a broken [/list] in there, which is what made me suspect it was a copy and paste job. :-D

Cheers for following up, it's more effort than a lot of moderators I have known over the years put in, but I suppose we should stop this talk before we go :offtopic: :rofl:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: Hide/show a text box based on drop down list in php

Post by JayBird »

mchaggis wrote:Looks like the greasemonkey script is broke cos it put a broken [/list] in there, which is what made me suspect it was a copy and paste job. :-D
That's just phpBB trying to be clever....and FAILING :lol:

GM is fine, just checked
Post Reply