Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show sections of a HTML form

I have a HTML form which when submitted sends an email with all the questions and answers using PHP.

I need to break the form down as it's too long. I need only one submit button and for the form to load only once. This obviously means that I need to use JavaScript show/hides.

I've tried using many different types but I can't get them to work properly with my form. It is quite large and seems to be very complicated to get to work with show/hide :(

I've used show/hide divs before but not tables.

Anyone have anything that can help?

What I'd like is,

  • A next button that takes you to another section of the form.
  • Whilst on the next section you can return back to the previous section or go on to another section again.
  • The last section to contain previous or submit.

Thanks in advance.

like image 365
John Vasiliou Avatar asked Mar 01 '12 13:03

John Vasiliou


People also ask

How do you hide paragraphs in HTML?

Use the hidden attribute in HTML5 to create a hidden paragraph in HTML5 i.e. an element no longer relevant.

How do you enclose a form in HTML?

Using the Form Tag The <form> tag is an HTML element that is used as a container to enclose other elements that can be considered as building blocks for forms. Some of these fundamental elements include the <label> tag, the <input> tag, and the <button> tag.

How do you hide a form field?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.


1 Answers

That's quite a common request. Here is one way

  • Break your form in pages using divs with easy to manage ids and only the first one visible

.

<form action=".." ..>
<!-- the first page has style set to be visible -->
<div id="formpage_1" style="visibility: visible; display: block; .."> 
  <label for="..">..</label>
  <input type=".." ..>
  ..
  <!-- NEXT button -->
  <input type="button" value="next" onclick="pagechange(1,2);">
</div>
<!-- the 2nd and following pages have style set to be invisible -->
<div id="formpage_2"  style="visibility: hidden; display: none; ..">
  <label for="..">..</label>
  <input type=".." ..>
  ..
  <!-- PREVIOUS and NEXT buttons -->
  <input type="button" value="back" onclick="pagechange(2,1);">
  <input type="button" value="next" onclick="pagechange(2,3);">
</div>
...
<div id="formpage_10"  style="visibility: hidden; display: none; ..">
  <label for="..">..</label>
  <input type=".." ..>
  ..
  <!-- PREVIOUS and SUBMIT buttons -->
  <input type="button" value="back" onclick="pagechange(10,9);">
  <input type="submit" value="Submit">
</div>
  • Use a simple JS function to switch between the pages

.

function pagechange(frompage, topage) {
  var page=document.getElementById('formpage_'+frompage);
  if (!page) return false;
  page.style.visibility='hidden';
  page.style.display='none';

  page=document.getElementById('formpage_'+topage);
  if (!page) return false;
  page.style.display='block';
  page.style.visibility='visible';

  return true;
}

Edit

If you want to use a table layout, break the for into more tables (one for each page) and give the ids to the tables instead of the divs

like image 195
Eugen Rieck Avatar answered Oct 13 '22 12:10

Eugen Rieck