Solved

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

Posted on 2009-07-09
4
295 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
4 Comments
 
LVL 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now