Results 1 to 3 of 3

Thread: [RESOLVED] Dynamic Form Generation Via PHP + MYSQL

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    34

    Resolved [RESOLVED] Dynamic Form Generation Via PHP + MYSQL

    Ok this is a issue i've been wrestling with for a few days now, and just for the life of me can not wrap my head around this.

    What I am wanting to do is Pull records from the MYSQL DB and create a dynamic form showing those records + fields so a user can update them.

    Now I can pull and place in the form with no problem. The problem comes in when saving the changes.

    There could be 50+ records that are pulled in from the DB. So that would create

    <input id="Name1"><input id="URL1">
    <input id="Name2"><input id="URL2">
    ...etc

    So obviously from record one, Name1, and URL1 are from that record.

    So how can I dynamically account for a ever changing list of veriables that come in and keep all the fields linked to the right record. Meaning so I don't have URL2 being updated for record 1 and so forth.

    Since this is one huge form, i'm at a loss.

    I'm wanting to do it this way, because before I had each record in it's own form. Which as you could guess required a Submit button for each field. And my users were a little annoyed with that setup. So one form so they can change all data that needs changed and update it all at once would be great.

    Any assistance would be greatly appreciated.

    Thank you.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Dynamic Form Generation Via PHP + MYSQL

    Do you have a unique row identifier on your table? In most cases, it's a must-have for any and every table. If you don't have one, you could add it like so:

    Code:
    ALTER TABLE table_name ADD rowid int(10) unsigned auto_increment, ADD PRIMARY KEY(rowid)
    This will give each of your rows a unique numeric identifier.

    One way you could do this is to use said identifiers in your form field names. Therefore, when an input is named "name1," it's not because this was the first one in your sequence, but because it's the row identified by 1 in the database. So when you call up your data, something like this will occur:

    Code:
    while($sql_row = mysql_fetch_array($sql_result)){
      echo "<input name=\"name".$sql_row["rowid"]."\" value=\"".$sql_row["name"]."\">";
    }
    When you process the submitted form, you'll have to strip off the "name" part, but what's left will be a unique ID to associate the data.

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    34

    Re: Dynamic Form Generation Via PHP + MYSQL

    I actually got it worked out.

    here is what I did.

    For each html form field name , I did the following. <input name="Name_' . $row[id] . '">

    Ok then the following on Submit

    Code:
    	if (isset($_POST)){
    		foreach ($_POST as $key=>$value) { 
    		  $tag = explode('_',$key);
                      //do something here
                    }
            }
    This effectively strips and parses the id number for each field so I can tell what row id that particular data belongs to.

Posting Permissions

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



Click Here to Expand Forum to Full Width