Solved

How to make form fields appear or not

Posted on 2001-06-25
12
170 Views
Last Modified: 2012-05-04
I have a "mail-to" form with various fields and drop-down choice menus.
The whole form gets mailed by way of a pearl mailform script. (standard free stuff)
How can I have a portion of the form (several fields) become visible only when a certain choice is made on one of the drop-down menus. Otherwise those fields stay hidden.
Keep in mind, the entire form (those fields included) must ship when the SUBMIT button is pressed.
bb
0
Comment
Question by:b6106b
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
If you need to do it cross browser.  Then the answer is no for netscape.

If you need it for IE the put the part you want invisible in a div with a style entry like this:

<div id="divname" style="visibility:hidden">

Form fields

</div>

The when you are ready to show them use an event liek teh onChange event of a select like this:

onChange="document.all['divname'].style.visibility='visible'"

HTH

Cd&
0
 

Author Comment

by:b6106b
Comment Utility
yes...it has to work on all browsers.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
AFAIK there is no way to do it in netscape.  It does not support the visibility attribute on form elements.

Cd&
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
Here is a workaround for you:

<html>
<head>
<script language="javascript">
<!--

function showHideLayer(selectObj){
  if (selectObj.selectedIndex == 1){
    if (document.all){
      document.all.mainLayer.style.visibility = "visible";
    }
    else{
      document.layers.nnLayer.visibility = "show";
    }
  }
  else{
    if (document.all){
      document.all.mainLayer.style.visibility = "hidden";
    }
    else{
      document.layers.nnLayer.visibility = "hide";
    }
  }
}

function getValues(formObj){
  var dummy1Obj = document.forms["dummy1"];
  var dummy2Obj;
  if(document.all){
    dummy2Obj = document.forms["dummy2"];
  }
  else{
    dummy2Obj = document.layers["nnLayer"].document.forms["dummy2"];
  }

  formObj.fieldA.value = dummy1Obj.fieldA.value;
  formObj.fieldB.value = dummy1Obj.fieldB.options[dummy1Obj.fieldB.selectedIndex].value;

  formObj.fieldC.value = dummy2Obj.fieldC.value;
  formObj.fieldD.value = dummy2Obj.fieldD.options[dummy2Obj.fieldD.selectedIndex].value;
 
  document.forms["actualFrm"].submit();
}

//-->
</script>
</head>
<body>
<form name="actualFrm">
<input type="hidden" name="fieldA" value="">
<input type="hidden" name="fieldB" value="">
<input type="hidden" name="fieldC" value="">
<input type="hidden" name="fieldD" value="">
</form>
<form name="dummy1">
<input type="text" name="fieldA" value="">
<select name="fieldB" onChange="showHideLayer(this);">
<option value="0">Off</option>
<option value="1">On</option>
</select>
</form>
<script language="javascript">
<!--
if (document.all){
 document.write("<div id=\"mainLayer\" style=\"visibility: hidden;\">");
}
else{
 document.write("<layer id=\"nnLayer\" visibility=\"hide\">");
}
//-->
</script>
<form name="dummy2">
<input type="text" name="fieldC" value="">
<select name="fieldD">
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
</form>
<script language="javascript">
<!--
if (document.all){
 document.write("</div>");
}
else{
 document.write("</layer>");
}
//-->
</script>
<form name="dummy3">
<input type="button" value="ok!" onClick="getValues(document.forms['actualFrm']);">
</form>
</body>
</html>

It uses layers and forms - a bit messy because of browser compatibility, but it works in IE and NN4.x - and with a little bit more code it can work in Netscape 6 if you need it to.

:o)

Ant
0
 
LVL 1

Expert Comment

by:Ernest022699
Comment Utility
Don't go with a non-portable solution!

You're better off having three Pages, Page A with a form which asks the user the key questions and submits to a Perl CGI which decides whether to send Page B or Page C.  (This is assuming that those certain fields you mention appear only on Page B or on Page C.)

Althernatively, you could have the first Page's Perl program generate HTML on the fly.  That way you could have hidden fields, etc.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 15

Accepted Solution

by:
a.marsh earned 100 total points
Comment Utility
ermmmmm...please explain why my solution is "non-portable"????

Ant
0
 
LVL 17

Expert Comment

by:dorward
Comment Utility
You're using a mailto form? That isn't a very reliably solution. In fact its a very very UNRELIABLE method. Please see:

http://www.isolani.co.uk/newbie/mailto.html
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
Dorward, I thought that when I first read it - but they are simply referring to the fact the form simply collects info that gets emailed somewhere - it does say that the email is sent using a Perl script:

>>>The whole form gets mailed by way of a pearl mailform script.


:o)

Ant
0
 

Author Comment

by:b6106b
Comment Utility
Thanks...we'll give it a try.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Hmmm... B looks a little low considering the difficult of the probelm and the detal of the solution.  IMHO

Cd&
0
 
LVL 15

Expert Comment

by:a.marsh
Comment Utility
I was thinking the same......oh well....

:o\

Ant
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

15 Experts available now in Live!

Get 1:1 Help Now