Meaningless Drivel is fun!
The moose likes HTML, CSS and JavaScript and the fly likes Outputing multiple form fragments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Outputing multiple form fragments" Watch "Outputing multiple form fragments" New topic

Outputing multiple form fragments

Dan Miller

Joined: Nov 19, 2008
Posts: 3
I am creating a registration form. The form allows multiple (in theory, unlimited) registrants. So there is an "Add attendee" button, and clicking that button should create an additional copy of the different registration information (name, age, etc.)

I figured out how to dynamically add information to the page, based on

My problem is that the example on that page is just adding one text box. I have 7 fields, all with labels and various formatting information. So I didn't want to put all of that HTML code inside the main page - I wanted to abstract it into its own page and just include / call it.

My first attempt was with PHP, so I separated out the common code into its own PHP include file, and included it. That worked fine, except that I had no way to identify the ids / names of the fields - because the PHP of course is server-side, there was no way to pass in an incrementing counter to the included PHP file to output in the form names (first_name_1, first_name_2, etc.)

So then I thought I could do it in JavaScript, but now I'm trying to determine the best way to output ~30 lines of HTML code in this Javascript function. A ton of document.write or string concatenation doesn't seem the most elegant solutions.

So at this point I'm kind of stuck - anyone have any thoughts?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

You could create the elements by hand, or look at client-side templating, such as this one.

I show a way to do client-side templating yourself with jQuery in the "comprehensive example" for chapter 4 of jQuery in Action (2nd Ed). The sample code is free at the book's page.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Outputing multiple form fragments
It's not a secret anymore!