• 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
https://www.neowin.net/forum/topic/1199661-creating-forms-dynamically/
Share on other sites

9 answers to this question

Recommended Posts

  • 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.

  • 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.

  • 0
  On 08/02/2014 at 11:22, Lingwo said:

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.

 

  On 08/02/2014 at 16:29, roosevelt said:

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 :)

  • 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.

  • 0
  On 08/02/2014 at 22:22, Lingwo said:

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 :)

  • 0
  On 08/02/2014 at 22:51, ncc50446 said:

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.

  • 0
  On 08/02/2014 at 23:57, Lingwo said:

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?

  • 0
  On 09/02/2014 at 02:50, ncc50446 said:

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.

  • 0
  On 09/02/2014 at 18:09, Lingwo said:

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

This topic is now closed to further replies.
  • Posts

    • I can't believe it was only two years since the last time MS raised their prices and now they think they can do it again so soon. Of course they needed to backtrack on this. I hope Nintendo is next.
    • I guess I gotta be even more specific then since I'm looking for an absolute confirmation.  The mini PC has never booted up or powered on by me. I'm assuming it'll be like buying a new PC from a store. Nothing is set up. Like absolutely nothing on the mini PC because I can't emphasize this enough. My USB drive already has Windows set up on it from my other desktop so I know it's clean. So now I connect it to the mini PC before it's even booted up or go to the desktop. I then turn on the mini PC and boot into the USB drive. I should see an existing partition. I'll delete it and then install Windows again over the entire drive. I don't necessarily want to create a separate partition. Once it installs and boots to the desktop, I'm assuming the drive is "clean" and free of any malware/viruses. Basically, this process will eliminate any potential for malware/viruses to still somehow hide in the SSD and then potentially affect me later.  I'm being very anal about this because I really don't trust anything bought from AliExpress, but the price was very tempting.  Is Windows Defender reliable? Afterwards, if I do a full scan and it doesn't find anything in the SSD and the USB drive, can I be confident the mini PC is now safe and secured to enter my banking credentials? If not, what else can I do to be absolutely certain?
    • I got an ESR Qi2 vent charger for my car and it's fantastic. It's very strong magnet and holds the phone securely over the worst potholes/roads, and my iPhone does charge. In Summer, the AC blows on the vent keeps and keeps the phone cold. In winter, I just close that vent, so it still stays cold. I agree with you on the Android front. Why not just build in the magnets like Apple does? It's frustrating to buy a case that adds magnets.
    • Hey, I've been using Windows since I was a little tyke. But for the last 15 years, I have been using Linux. I'm using EndeavourOS/Arch atm. wipe it, as in deleting anything from the SSD in the computer. Make a new partition. USB booting device should be free of malware, as long as you download it directly friom MS. Not some XYZ company.
    • "Nuking it" is slang for doing a clean install. USB installation media... delete the partition... start fresh.
  • Recent Achievements

    • Collaborator
      fernan99 earned a badge
      Collaborator
    • Collaborator
      MikeK13 earned a badge
      Collaborator
    • One Month Later
      Alexander 001 earned a badge
      One Month Later
    • One Month Later
      Antonio Barboza earned a badge
      One Month Later
    • Week One Done
      Antonio Barboza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      581
    2. 2
      ATLien_0
      216
    3. 3
      Michael Scrip
      169
    4. 4
      Xenon
      136
    5. 5
      +FloatingFatMan
      125
  • Tell a friend

    Love Neowin? Tell a friend!