Jump to content

Dynamic form generation from Database


powens

Recommended Posts

I'm praying there's a function somewhere out there that:

 

  • generates a html form based on the fields of a particular table within a DB (namely mySQL)
  • generates the appropriate form input based on the fields type or notes (ie. for field: avatar_img, it knows to generate a file input)
  • uses the POST method to submit
  • generates and submits SQL insert query to database

 

In addition to that, a similar function that generates a form that allows the editing of records.

 

It doesn't take too long to write these each time myself, but if there's a function that you provide the formname, database driver, table name, whether your form is to insert or edit, that would be quite nifty i think. 

 

ie.  generateForm(addUser, mySQL, tUsers, insert)

 

Seen any such thing?

Many thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/224038-dynamic-form-generation-from-database/
Share on other sites

I just had a similar situation where I had loads of SQL fields and I needed to provide submission and editing + some control panel features.

 

The code isn't really in that shape that I would recommend anyone to re-use it. But it gets the job done very nicely.

 

Basicly this is what I ended up doing:

- Started the form creation with SQL statement DESCRIBE table_name

- Loop trough those fields and check what type they are and put them in array. (I had another array of fields that were not supposed to show done manually)

- (Now someone fills up the form and submits)

- Same file checking the form and it always started with DESCRIBE table

- But now when there is a submission, Simply loop through the fields and do some validation

- The simplest thing was actually generating the SQL statement in a foreach loop.

 

That's pretty much it. I also ended up doing like 4-5 helper functions which I called to fill up the form. They were pretty simple. Just like function isChecked($fieldname, some other args that were specific for the job).

 

But I think there's some idea.. If I only had inspiration to make it a full polished class =) Now I only have basicly functions and a filter class.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.