Solved

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

Posted on 2009-07-09
4
321 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:nick2253
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24816674
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
 
LVL 19

Accepted Solution

by:
erikTsomik earned 500 total points
ID: 24816736
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
 
LVL 9

Expert Comment

by:rg20
ID: 24816874
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
 
LVL 9

Author Closing Comment

by:nick2253
ID: 31601758
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

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question