SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form with variable number of fields

    I was thinking, what is the best way to manage a form with a variable number of fields?

    Suppose you have the fields "phone1", "phone2" and "phone3", how can I insert the data into a database, not knowing how many fields I need to insert before the form gets submitted?

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Question: Do you need to be able to search on said data?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Question: Do you need to be able to search on said data?
    In which sense?

    If I understood what you meant then no, data would just be inserted in a database table.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    per your example, rather than phone1, phone2 etc, create them as an array phone[]. then you can loop thru then when processing

  5. #5
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by litebearer View Post
    per your example, rather than phone1, phone2 etc, create them as an array phone[]. then you can loop thru then when processing
    You mean that the field name should be "phone[]"?

    Code:
    <input type="text" name="phone[]" />
    <input type="text" name="phone[]" />
    <input type="text" name="phone[]" />

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Which then returns all phones in an array ($_POST['phone'][0], etc). You could save them in a single database field by imploding them on an unused character (something other than a number) after sanitizing them.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Which then returns all phones in an array ($_POST['phone'][0], etc). You could save them in a single database field by imploding them on an unused character (something other than a number) after sanitizing them.
    Could you please explain the section in bold?

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    imploding the entries of the array makes it a single string with a given delimiter. single string = single field, no matter how many phone numbers there are.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    imploding the entries of the array makes it a single string with a given delimiter. single string = single field, no matter how many phone numbers there are.
    Ok, thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •