Storing dynamically generated fields

  • Started
  • Last post
  • 8 Responses
  • Diaz

    Hi!

    I'm making a form and storing the data using MySQL. Nothing fancy, but there is one part of the form that I just don't understand how to store the information.

    The image below shows a snapshot of what I mean. When a user clicks on the plus icon one more of the field next to it will appear. Im doing this with jQuery. I have made the front end work, now I just need to be able to store the data dynamically. Depending on how many (in this case) artist you want to add you will be able to store the data.

    How do I store the data from dynamically added fields?

    Here is an image:

    Thanks in advance,
    Alonso Diaz

  • Daithi0

    Why not just show/hide the new field with Jquery? I'm working on something similar at the moment and that's how I'm probably going to do it.

    • show/hide (instead of adding them dynamically)Daithi
    • I've already go the front end working. This adds a new set of "namn" and url field and puts a unique name for it.Diaz
    • The thing is that it has to be scalable.Diaz
  • TheFatBaron0

    If you're limiting to a max of 2-3 items, it might be easier to use Daithi's suggestion. Otherwise, if you need a potentially infinite amount of fields, you could combine everything into an array, then store that array in a MySQL cell, then when you pull it back out, you can sort the array as usual.

    • Ahhh Nice! I'll try that. Thanks!
      Thanks Daithi. If everything fails, I'll just do as Daithi said.
      Diaz
  • Diaz0

    I'll try the infinite amount first and if that fails. I'll just do it as Daithi said :)
    Thanks guys!

  • Samush0

    if the data needs to be searchable or the individual field contents will need to deleted exclusive of each other you'll want to store them in separate MySQL rows.

    you can use jQuery to clone the fields already on the page and the clones can be given a slightly different name to their parent.
    e.g.
    name="url-0" -- original field
    name="url-1" -- clone 1
    name="url-2" -- clone 2
    ...

    then in your server-side code that processes the form you need to loop through all the form fields and pull out the data from the ones whose 'name' (attribute from html POV) or 'key' (array key from server-side POV) begins with "url"

    hope that makes sense :-/

  • dijitaq0

    you can also name the input fields with brackets in the end, name="name[]", it will convert all the data from the fields with the same name into an array.

    store the data while looping through the array.

    • oh brilliant! i never knew that.

      i'd just like to thank university for doing a sterling job!
      Samush
    • Ohh!! How could I've forgotten that...duh! ofcourse... store the array in the name. thanks dijitaqDiaz
  • acescence0

    in database terms this is called one to many. you have a table that's users, and a table that's urls, with one of the fields being the id that refers to the associated row in the user table.

  • acescence0

    also, this thing makes development super easy for simple data needs. you create your objects and set up their relationships, it does parent/child and many-to-many relations. the generated code handles all of the SQL and you use simple new, save, and get methods, etc..

    http://www.phpobjectgenerator.co…

  • lukus_W0

    One way of achieving this would involve creating 2 extra tables.

    One table to store the extra values, and another to link your main table with the newly created extra values that have been created.

    The link table contains the ID of the record in your main table and the ID of the record in the 'extra values' table.

    Each time the user adds an extra field, you add a record to the 'extra values' table, and update the link table to associate it with the current main record.

    When it comes to reading your data, you can create a query which collates the data in your main table together with those relevant, matching records from your 'extra values' table.