• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Outputing multiple form fragments

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic