This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Outputing multiple form fragments" Watch "Outputing multiple form fragments" New topic
Author

Outputing multiple form fragments

Dan Miller
Greenhorn

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

http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/



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
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Outputing multiple form fragments
 
Similar Threads
Complex form creation
next upgrade in a few days
Exception questions
Complex form creation
Writing to a URLConnection object