Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get form items to store in mysql #30

Closed
q2amarket opened this issue Nov 30, 2015 · 18 comments
Closed

How to get form items to store in mysql #30

q2amarket opened this issue Nov 30, 2015 · 18 comments

Comments

@q2amarket
Copy link

I am trying to get json formate of formBuilder and want to store into mysql database. Is there any way to do? There is no enough information on your docs. Thanks a lot

@Goddard
Copy link

Goddard commented Dec 30, 2015

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

@ivuorinen
Copy link
Contributor

+1 from me too.

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

@demitrol
Copy link

demitrol commented Apr 4, 2016

+1

2 similar comments
@dmitri-wm
Copy link

+1

@dsignr
Copy link

dsignr commented Apr 11, 2016

+1

@AminMkh
Copy link

AminMkh commented Apr 11, 2016

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

@KareemJaper
Copy link

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

@AminMkh
Copy link

AminMkh commented Apr 11, 2016

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

@AminMkh
Copy link

AminMkh commented Apr 11, 2016

@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 !

@KareemJaper
Copy link

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

@KareemJaper
Copy link

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

@AminMkh
Copy link

AminMkh commented Apr 12, 2016

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

@dmitri-wm
Copy link

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.

@bluespore
Copy link

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

@ameeradnan
Copy link

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

thanks in advance

@beytarovski
Copy link

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 😉

@him-developer
Copy link

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"]);  

@kevinchappell
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests