Solved

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

Posted on 2009-07-09
4
316 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

762 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