Hide/show a text box based on drop down list in php
Moderator: General Moderators
Hide/show a text box based on drop down list in php
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
or
the code to display another drop down when particular value is selected in drop down list in php
Re: Hide/show a text box based on drop down list in php
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:
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:
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
Code: Select all
<?php if ($_REQUEST['nameofdropdown'] == 'somevalue') { ?>
<select name="blah">
...
</select>
<?php } ?>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>
Hope that helps
Re: Hide/show a text box based on drop down list in php
I tried the code u given but it is not working at all.
Re: Hide/show a text box based on drop down list in php
<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:
<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 .
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:
<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 .
Re: Hide/show a text box based on drop down list in php
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:
Alternatively, but the div inside the td:
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:
<input name="r_place2" type="text" id="r_place6">
</span></td>
</tr>
</table>
</div>
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:
<input name="r_place2" type="text" id="r_place6">
</span>
</div>
</td>
</tr>
Re: Hide/show a text box based on drop down list in php
So many things to say:
PHP is a server-side language, not client-side. User interaction with a page is handled with Javascript. Therefore...
Please use [ code=javascript ][ /code ] tags as it's MUCH easier to read.
PHP is a server-side language, not client-side. User interaction with a page is handled with Javascript. Therefore...
I'm moving this thread there.[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.
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]
Reason: Removed errant [/list]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Hide/show a text box based on drop down list in php
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.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...
I'm moving this thread there.[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.
Please use [ code=javascript ][ /code ] tags as it's MUCH easier to read.[/list]
Re: Hide/show a text box based on drop down list in php
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.
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.
Re: Hide/show a text box based on drop down list in php
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. 
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

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
Re: Hide/show a text box based on drop down list in php
That's just phpBB trying to be clever....and FAILINGmchaggis 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.
GM is fine, just checked