Welcome to Dream.In.Code
Become an Expert!

Join 150,406 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 929 people online right now. Registration is fast and FREE... Join Now!




Form visibility

 
Reply to this topicStart new topic

Form visibility, forms with drop down menu

b_real
30 Mar, 2008 - 07:03 AM
Post #1

New D.I.C Head
*

Joined: 29 Dec, 2006
Posts: 12


My Contributions
Not entirely sure what the terminology is for what Im trying to find out, but rough description is as follows:

On one page, I want a form that the user can use to fill in details of a product which gets sent to a database using a perl script. My query is about the form on the one page - I want to have it so that the user first chooses what type of product they want to enter (book, cd, car, bike etc) then based on what is selected from the drop down list, the form will change to reflect the attributes of the product (so if a book is chosen, he can then enter title, author etc, and if a car is chosen, his options change to colour, engine size etc.. ).

At the minute ive got a form for each product on the one page, but it looks terrible. Trying to condense it and make it a bit less cluttered.

Can this be done just using html? What should I be searching for?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Form Visibility
30 Mar, 2008 - 07:45 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well this can't be done with just HTML alone, it is actually a combination of HTML, Javascript and CSS (cascading style sheets).

What you will want to search for are the CSS attributes "visibility" and "display". You will also want to research the Javascript method "getElementById()"

Here is an example you can try yourself below. Just cut and paste the code and save it as an HTML page on your desktop.

CODE

<html>
<head>
<title>Show/Hide Elements</title>
<script type="text/javascript">

function showhide() {
    // Get a reference to your form's id
    var form = document.getElementById("myform");

    // If it is hidden, show it, otherwise hide it.
    if (form.style.visibility == "hidden") {
        form.style.visibility = "visible";
    }
    else {
        form.style.visibility = "hidden";
    }
}

</script>
</head>

<body>

<form method="POST" action="" id="myform">
<textarea name="comments" rows="3" cols="45"></textarea>
</form>

<!-- Call the showhide() javascript function -->
<input type="button" name="showhide" value="Show/Hide" onclick="showhide()"/>

</body>
</html>


Clicking the button should hide and show the form above. The display attribute will tell the page if it should be included in the natural flow of the page or not. You can toggle this as "block" or "none".

So research what you see in this example and you should get an idea of what you need to do.

Enjoy!

"At DIC we be hiding and showing form code ninjas! Just some of our forms are too ugly to be seen in public." decap.gif
User is offlineProfile CardPM
+Quote Post

spearfish
RE: Form Visibility
31 Mar, 2008 - 03:57 PM
Post #3

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
Martyr strikes again!

How I would do this would be to set the sub-forms for all products to a hidden visibility, then do something very similar to Martyr's code.

An onChange handler would probably be the best here, where the visibility of the sub-forms is reevaluated each time the drop down list is changed, and then using a switch or if statements, check which sub-form should become visible. If that makes any sense.
User is offlineProfile CardPM
+Quote Post

polymath
RE: Form Visibility
4 Apr, 2008 - 05:10 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 451



Thanked: 5 times
Dream Kudos: 500
My Contributions
Martyr is right!

Another approach is useing if...then statements to run the code you want in javascript.
Also, you may want to use a script that you can directly encode into your page that can also interact with a database (such as PHP) in the future.
Thats just personal preference, though.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month