How can a web form drop-down list spawn a text field?

I am currently running a website that has an application form that gets submitted via email via PHP.  I would like to add to this form a drop down list that, depending on the choice in the list, will spawn new text fields.  For example, the drop down might list:

Your favorite food:
          Hot Dog
          Pizza
          Salad

If the user selects "Salad", it would spawn a text field like "Your favorite dressing", letting the user type in their favorite dressing.  If the user selects "Pizza" it would spawn three text fields like "Most favorite topping", "Second favorite topping", and "Most hated topping", where the user would type in the toppings as applicable.

I would like to be able to do this in JavaScript or in PHP.  I do not want to have to install another language on my server.  (Obviously HTML can be used.)

Also, I am pretty sure I can figure the submission for the fields out on my own (since I do it already for the other fields).  My question really is getting this "if-then" behavior implemented.

Thanks,
Nick
LVL 9
nick2253Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This is typically an AJAX application, however if you have fairly static fields, you might try it with just HTML and CSS.  Look into using jQuery - it will probably be the lightest weight solution.  Best, ~Ray
0
erikTsomikSystem Architect, CF programmer Commented:
try something like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<SCRIPT language=JavaScript>
	function selectMenu () {
		var menu =document.getElementById('food').selectedIndex;
		if (menu==2) {
			document.getElementById('pizza').style.display='block';
 			}
			else {
				document.getElementById('pizza').style.display='none';
				}
		if (menu==3) {
			document.getElementById('salad').style.display='block';
 			}
			else {
				document.getElementById('salad').style.display='none';
				}
		
		}
</SCRIPT>
 
</HEAD>
 
<BODY BGCOLOR="#FFFFFF">
<form name="Login" action="text4.cfm" method="post">           
      Your favorite food:
			<select name="food" id="food" onchange="selectMenu(this);">
				<option value="">PLease make a choice</option>
				<option value="1">Hot dog</option>
				<option value="2">Pizza</option>
				<option value="3">Salad</option>
			</select>
			<div id="salad" style="display:none;">
				your favorite Dressing:<input type="text" name="dress" value=""/>
			</div>
			<div id="pizza" style="display:none;">
				Most favorite topping<input type="text" name="mtop" value="" /><br />
				Second favorite topping<input type="text" name="stop" value="" /><br />
				Most hated topping<input type="text" name="mhated" value="" />
 		</div>
</form>
 
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rg20Commented:
This site has a good writeup on doing with Javascript and DHTML
http://www.daniweb.com/forums/thread142757.html#

What is basically does is when you put an onchange button, you can call a function like the one below to dynamically enter a row into the HTML.

You would have to do some prep-work to define the area you want it inserted

function generateRow() {
 
var d=document.getElementById("div");
d.innerHTML+="<p><input type='text' name='food'>";
 
}

in the code below, was copied from the above URL

The getElementById("div") tells HTML to look in the HTML code and put the text in there
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input name="food" type="text" id="food" />
</label>
<p>
<input name="food" type="text" id="food" />
</p>
<p>
<input name="food" type="text" id="food" />
</p>
<div id="div"></div>
<p><input type="button" value="Add" onclick="generateRow()"/></p>
<p>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>

Open in new window

0
nick2253Author Commented:
Thank so much for your prompt answer!  It was thorough, and was spaced well so it was easy enough for me to understand.  I have been able to adapt your solution to fit my exact problem perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.