Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-09
4
Medium Priority
?
335 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 111

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 2000 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

Industry Leaders: 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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

609 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