Giter VIP home page Giter VIP logo

Comments (18)

bluespore avatar bluespore commented on June 5, 2024 3

+1

Much like @dmitri-wm I also discovered https://github.com/dobtco/formbuilder/ but it looks to be abandoned, so I have a preference to invest in a repo that is more actively developed, like yours @kevinchappell, but the JSON export is crucial for me.

from formbuilder.

AminMkh avatar AminMkh commented on June 5, 2024 2

I'm parsing the XML with PHP and storing it in MySQL. I use the following code:

$form_data = $_POST['form_data'];
$xml = simplexml_load_string($form_data);
$result = [];
if (isset($xml->fields) and isset($xml->fields->field)) { // make sure the form is not empty !
      foreach ($xml->fields->field as $record) { // loop through every field
           $array = (array)$record; // covert it to an array

           $record = current($array);  // get the field
           $record['options'] = next($array); //additional options, needed for radio buttons, select lists...etc

           $result[] = $record;
      }
}
return $result;

The result would be an array that you can loop through and store every field in the database. Like this

If you want to convert the XML to JSON, using PHP you can, until the plugin can support exporting directly.

        $xml = simplexml_load_string($form_data);
        $result = [];
        $json = json_encode($xml)

My site is using it here

from formbuilder.

him-developer avatar him-developer commented on June 5, 2024 2

I have tried all the PHP functions mentioned above to parse XML to JSON but none of them turned out a success.
As most of above PHP code uses simplexml_load_string which returns a set of XML objects which is not very easy to parse and it become worse when you need to parse options of radios, select and checkbox. print_r and var_dump do not give full picture of the object tree you got from simplexml_load_string.
Finally I have decide to convert XML to JSON on client side and then simply post the JSON only.
I have used below library from @abdmob for conversion to json.
https://github.com/abdmob/x2js

var x2js = new X2JS();
var xml_str = fbTemplate.value;
var form_json =  x2js.xml_str2json(xml_str); 
form_json = JSON.stringify(form_json["form-template"]["fields"]["field"]);  

from formbuilder.

beytarovski avatar beytarovski commented on June 5, 2024 1

Hey guys, we created a quick function for it 😌

function fn_parse_form_data( $xml ) {
    $fields = array();
    $xml = simplexml_load_string( $xml );
    $xml = $xml->fields;

    if( isset( $xml->field ) ) {
        foreach( $xml->children() as $field ) {
            $name = (string) $field['name'];
            $fields[$name] = array();

            foreach( $field->attributes() as $k => $v ) {
                $fields[$name][$k] = (string) $v;
            }
        }
    }
    return $fields;
}

Example usage:

fn_parse_form_data( '<form-template><fields><field class="form-control text-input" label="Your name" name="text-1462909445840" placeholder="Your name" type="text" subtype="text"></field><field class="form-control text-area" label="Your message" name="textarea-1462882789673" placeholder="Enter your message" type="textarea"></field></fields></form-template>'  );

It should return:

Array
(
    [text-1462909445840] => Array
        (
            [class] => form-control text-input
            [label] => Your name
            [name] => text-1462909445840
            [placeholder] => Your name
            [type] => text
            [subtype] => text
        )

    [textarea-1462882789673] => Array
        (
            [class] => form-control text-area
            [label] => Your message
            [name] => textarea-1462882789673
            [placeholder] => Enter your message
            [type] => textarea
        )

)

Best 😉

from formbuilder.

Goddard avatar Goddard commented on June 5, 2024

+1

would be great if I could add this to my php web app.

I could store it as XML I suppose, but not as simple to deal with as maybe json.

from formbuilder.

ivuorinen avatar ivuorinen commented on June 5, 2024

+1 from me too.

JSON format could also fix #109, #111 and #112.

from formbuilder.

demitrol avatar demitrol commented on June 5, 2024

+1

from formbuilder.

dmitri-wm avatar dmitri-wm commented on June 5, 2024

+1

from formbuilder.

dsignr avatar dsignr commented on June 5, 2024

+1

from formbuilder.

KareemJaper avatar KareemJaper commented on June 5, 2024

Nice solution @AminMkh , but i can't get option values ?

from formbuilder.

AminMkh avatar AminMkh commented on June 5, 2024

@TeqExperts the plugin doesn't seem to support that, you can only save the options keys. It doesn't support a few other things like text fields default value, Just place holders for now.

from formbuilder.

AminMkh avatar AminMkh commented on June 5, 2024

@TeqExperts actually I just checked the XML, it is there, it seems lost when im parsing it. I'll update my code, thanks a lot !

from formbuilder.

KareemJaper avatar KareemJaper commented on June 5, 2024

That's great. ;) just share the code for me after updating, thank you !

from formbuilder.

KareemJaper avatar KareemJaper commented on June 5, 2024

@AminMkh I tried to get values but i can't, how did you do it?

from formbuilder.

AminMkh avatar AminMkh commented on June 5, 2024

@TeqExperts I couldn't parse it either, whenever I use

simplexml_load_string($form_data);

The option values are lost, I will try to find another PHP parser. I'll keep you posted.

from formbuilder.

dmitri-wm avatar dmitri-wm commented on June 5, 2024

Sorry, don't know if it's ok to past link for ext project. But I've spent the whole day to find pretty formbuilder which works out of the box and provides json data (I'm using postgres json). http://dobtco.github.io/formbuilder/ , could not find repo with project so copied all files from this sample.

from formbuilder.

ameeradnan avatar ameeradnan commented on June 5, 2024

@AminMkh do let us know if you updated the parser...i'm kinda lost looking for this solution

thanks in advance

from formbuilder.

kevinchappell avatar kevinchappell commented on June 5, 2024

There are a number of ways to accomplish data prep for your backend, which one depends on your specific app or website. I'm closing this issue as the discussion has strayed too far from topic and the topic is outside of formBuilder's scope.

from formbuilder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.