cofcmgr
asked on
PHP code to hide/show text box based on answer to previous drop down list
I am looking for php code that will enable me to hide/show a text box based on the users previous drop down list response.
(1) I want the "additional" text box to ONLY display if the user answers the drop down list with "yes"
(2) If the user answers "no" to the previous drop down list I don't want the "additional" text box displayed
(3) When the form is originally opened I dont want it to display the "additional" text box.
Thank you very much!
(1) I want the "additional" text box to ONLY display if the user answers the drop down list with "yes"
(2) If the user answers "no" to the previous drop down list I don't want the "additional" text box displayed
(3) When the form is originally opened I dont want it to display the "additional" text box.
Thank you very much!
Hello,
You can't do that in PHP unless you want to reload the page everytimes you select a value in the listbox. What I suggest is to use Javascript, which will allow you to do exactly what you need, but dynamically.
This code will do exactly what you need:
(Don't forget to put the javascript in the <head></head> )
########################## #######
<script language="JavaScript" type="text/javascript">
function showTextBox(selectValue, objTextBox) {
switch(selectValue) {
case 'yes':
objTextBox.style.visibilit y = 'visible';
break;
case 'no':
objTextBox.style.visibilit y = 'hidden';
break;
}
}
</script>
<form>
<select id="select_choice" name="select_choice" onchange="showTextBox(this .value, this.form.tb_choice)">
<option value="0">Choose</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<input type="text" id="tb_choice" name="tb_choice" style="visibility:hidden" />
</form>
You can't do that in PHP unless you want to reload the page everytimes you select a value in the listbox. What I suggest is to use Javascript, which will allow you to do exactly what you need, but dynamically.
This code will do exactly what you need:
(Don't forget to put the javascript in the <head></head> )
##########################
<script language="JavaScript" type="text/javascript">
function showTextBox(selectValue, objTextBox) {
switch(selectValue) {
case 'yes':
objTextBox.style.visibilit
break;
case 'no':
objTextBox.style.visibilit
break;
}
}
</script>
<form>
<select id="select_choice" name="select_choice" onchange="showTextBox(this
<option value="0">Choose</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<input type="text" id="tb_choice" name="tb_choice" style="visibility:hidden" />
</form>
I had a typo in part of my post. I left out an equal sign when I needed 2.
<select onchange="document.getElem entById('a dditionalB ox').style .display = (this.value=='yes')? '' : 'none';">
bol
<select onchange="document.getElem
bol
ASKER
I understand what you are saying about using javascript. The values that are captured in the form I need to pass forward to additional pages, and email. The PHP form that I would like to use in place of "additionalbox" is:
FormFieldLabel("Reseller (If Selected)", $_POST["Reseller"], "1", "1", $_POST["Target"]);
echo "<br> ";
FormText("Reseller", $_POST["Reseller"], "50", "50");
echo <<<END
Is that possible?
FormFieldLabel("Reseller (If Selected)", $_POST["Reseller"], "1", "1", $_POST["Target"]);
echo "<br> ";
FormText("Reseller", $_POST["Reseller"], "50", "50");
echo <<<END
Is that possible?
What is FormFieldLabel and FormText? They aren't built-in PHP functions to my knowledge. If it is some function or routing that makes the fields then that function or routine would need to support this. I would need to know more about them and see code to know if that is possible. Most likely it is but the PHP you showed me isn't enough to know for sure or say how to do it.
bol
bol
By the way, if the show/hide is done without the page being "refreshed" then none of what we are doing will effect what is sent to other pages or emailed. It is all Javascript and would leave the info in the form intact and unchanged. The page is what is changed.
bol
bol
ASKER
Ok, I think I am getting closer to answer. I want to take the value that is entered into the input box that we created and becomes visible when the user selects "yes" and pass it to other forms.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just a slight correction:
if (isset($_POST['additionalB ox']))
will always be true - it will always be set (assuming noone is messing with your form), it just may be empty
if (isset($_POST['additionalB ox']) && trim($_POST['additionalBox ']) != "")
is what you need :)
if (isset($_POST['additionalB
will always be true - it will always be set (assuming noone is messing with your form), it just may be empty
if (isset($_POST['additionalB
is what you need :)
I'm glad I could help. Thanks for the grade, the points and the fun question.
bol
bol
PHP can be used to write the Javascript and send it to the browser. If you provide your PHP then I can help you do this in PHP but the html/javascript you need is like ...
<select onchange="document.getElem
and the input would look like ...
<input type="text" name="additionalBox" id="additionalBox" style="display: none;">
That will change the box depending on what is selected and the box will not be shown by default (i.e. when page loads).
Let me know if you have a question or need more info.
bol