• 0

Creating Forms Dynamically


Question

Hey,

I'm working on a project, where I need to be able to create forms dynamically. An example is: http://phpformgen.sourceforge.net

The thing is though, I need the source code to look at lol

I'm trying to figure out how to create my own..I think I understand enough, store it all in arrays...The problem though is...storing it in the database, not sure the easiest way...And also how to move fields...Say you create a text field, last_name, and you forgot to create first_name...So you create that one next...But how to I rearrange the arrays to move first_name before last_name?

And do I even have the right idea? lol

Thanks for any assistance

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Wouldn't happen to have the code to share? lol

 

No, sorry. I wrote this within Codeigniter so it isn't that simple to give you the code.

If you get stuck, i can post snippets.

 

Aim to get the form working where a user can create a form input of their choice.

Then store that in the DB. Don't store the form input HTML. Just the form options like type = text.

Hopefully FuelPHP allows you specify an array to generate the form input.

Link to comment
Share on other sites

  • 0

What you can do is store your data in an array and then use the json_encode() function to convert the PHP array to a JSON formatted string. This will make it lot easier to save the codes in the database. And then just call json_decode() to revert the JSON string to PHP array.

Link to comment
Share on other sites

  • 0

Something like this would do the trick

http://code.google.com/p/php-form-builder-class/

 

If you're starting a new project though. I'd look into using a framework as they usually have form builders and even validation libraries for form inputs.

I'm currently using FuelPHP. It does have form building, but I'm not sure how to use it to make something as dynamic as my example. Your example isn't quite as dynamic as my example. These people know nothing about HTML. I need them to see a page, and select what kind of input they want. Then select the name of that field. Then select another field, and a name for that one. All without inputting any code. Has to be really simple.

 

What you can do is store your data in an array and then use the json_encode() function to convert the PHP array to a JSON formatted string. This will make it lot easier to save the codes in the database. And then just call json_decode() to revert the JSON string to PHP array.

Thanks, I'll take a look into that :)

Link to comment
Share on other sites

  • 0

Come to think of it. I've actually done something very similar to what you want.

All i did was show the user a drop down of the various inputs, they select one and then give it a unique name.

Then i store what i need in the database. Like what Roosevelt said but i serialised the data instead. No difference really. JSON might be nicer if you want to use the data outside of PHP.

 

For the view i just loop through the data and generate the inputs. I've not used FuelPHP so i don't know how its form helper works.

Link to comment
Share on other sites

  • 0

Come to think of it. I've actually done something very similar to what you want.

All i did was show the user a drop down of the various inputs, they select one and then give it a unique name.

Then i store what i need in the database. Like what Roosevelt said but i serialised the data instead. No difference really. JSON might be nicer if you want to use the data outside of PHP.

 

For the view i just loop through the data and generate the inputs. I've not used FuelPHP so i don't know how its form helper works.

Wouldn't happen to have the code to share? lol

Been doing some reading..Looks to be more work than I originally thought lol But, I suppose it is teaching me to learn something new, which isn't always a bad thing..

And thanks for the help, I'll be keeping that in mind.

 

Thanks everyone for the help so far :)

Link to comment
Share on other sites

  • 0

No, sorry. I wrote this within Codeigniter so it isn't that simple to give you the code.

If you get stuck, i can post snippets.

 

Aim to get the form working where a user can create a form input of their choice.

Then store that in the DB. Don't store the form input HTML. Just the form options like type = text.

Hopefully FuelPHP allows you specify an array to generate the form input.

FuelPHP based lots off Codeigniter, so snippets should be able to help

How do you suggest doing that? Converting the output of the form into say something like 'type=text,name=random_name,label="First Name";' or something like that?

Link to comment
Share on other sites

  • 0

FuelPHP based lots off Codeigniter, so snippets should be able to help

How do you suggest doing that? Converting the output of the form into say something like 'type=text,name=random_name,label="First Name";' or something like that?

 

I would look at something like this.

http://fuelphp.com/docs/classes/fieldset/fieldset.html

 

Your DB table would be something like

| ID | input_name | properties |

 

properties would be a JSON or serialised array of all the properties (type = text, etc )

 

The fieldset class seems to a quick way to generate form inputs based off an array. It can also handle your labels/validation too.

Link to comment
Share on other sites

  • 0

I would look at something like this.

http://fuelphp.com/docs/classes/fieldset/fieldset.html

 

Your DB table would be something like

| ID | input_name | properties |

 

properties would be a JSON or serialised array of all the properties (type = text, etc )

 

The fieldset class seems to a quick way to generate form inputs based off an array. It can also handle your labels/validation too.

Thank you for the help :) I'll be starting this next week...It seems they have something more important to work on first, so next week I have to get to work on this. Though I'll most likely play around with this in a couple days. Maybe even later tonight.

 

Thanks for all the help :) Should be a fun little project

Link to comment
Share on other sites

This topic is now closed to further replies.