Forums

Struggling with Data Save

Matt2012 28 Oct, 2014
Banging my head about this - been struggling for hours feel like this should be easier.

I have a db save in the onSubmitEvent in the ServerValidation success event.

This is what gets saved

 INSERT INTO `jm3tb_chronoengine_chronoforms_datatable_myTable` (`isRequiredForWork`, `yourSearchTopicOrQuestion`, `specificSearchTerms`, `dateRequired`, `typeOfInformationRequired`, `keyPapersAuthorsOrSources`, `user_id`, `uniq_id`, `created`) values ('1', 'fdfgd', '', '30/10/2014', '', '', '373', 'faf96d539d61439d5fe65f423d4c4f01c6b0b910', '2014-10-28 15:00:51');


You can see this is only a few of the fields.

Notice nothing in the arrays UserData or cbUserData are being saved.
I tried to use custom code to add data to the top array to be saved.
Notice there is email that is populated but its not being saved.
I created the table using the table creator. I renamed the ID although its not embedded in an article.

This is the debug on the form

Array
(
    [chronoform] => myTable
    [event] => submit
    [tvout] => ajax
    [users] => Array
        (
            [id] => 
        )

    [UserData] => Array
        (
            [name] => My Name
            [email] => Matt@myemail.co.uk
        )

    [cbUserData] => Array
        (
            [cb_organisationdrop] => Other - please specify below
            [cb_organisation] => mycompany
            [cb_phone] => 
        )

    [isRequiredForWork] => 1
    [yourSearchTopicOrQuestion] => fdfgd
    [specificSearchTerms] => 
    [dateRequired] => 30/10/2014
    [typeOfInformationRequired] => 
    [keyPapersAuthorsOrSources] => 
    [submitButton] => Send Request
    [email] => Matt@myemail.co.uk
    [id] => 14
    [ip_address] => 146.66.61.80
)
Matt2012 28 Oct, 2014
I've looked through all those links - I think it must have something to do with the different subArrays but I needed to do that to populate fields from the database - unless there is another way.

That means I have Mysql fields named UserData[name] not sure if that is valid or not?
I wouldn't no how else to do it as creating form values using custom code is also not saving .
Max_admin 28 Oct, 2014
Hi Matt,

You can configure the "db save" to "save data under model id", then set the model id as "cbUserData" and that would save the fields under this key only, so, in your case, you will have to clone all the fields values outside their sub keys, using PHP code in a "custom code" action before the "db save".

Then, if your table has fields matching those fields names it should work.

Using "UserData[name]" fields in your table will not work, this is only for form fields.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
Matt2012 28 Oct, 2014
Still no dice

Added this in custom code

$form->data['magic']['email'] = $form->data['UserData']['email'];


That appears in the array

Now in db save I say use Model ID and specify it as magic.

On Save It's still

INSERT INTO `jm3tb_chronoengine_chronoforms_datatable_myTable` (`user_id`, `uniq_id`, `created`) values ('373', '6ca4f869af413ee2c3ecb45d6b326b0cb5ae9049', '2014-10-28 16:32:25');
Max_admin 28 Oct, 2014
What's the name of the email field in your table ? this will have to be set in the data array:
$form->data['db_email_field'] = $form->data['UserData']['email'];
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
Max_admin 28 Oct, 2014
Then it should be:
$form->data['email'] = $form->data['UserData']['email'];
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
Matt2012 28 Oct, 2014
<?php
$form->data['magic']['email'] = $form->data['UserData']['email'];
unset($form->data['id']);
unset($form->data['magic']['id']);
$form->data['email'] = $form->data['UserData']['email'];
?>


Still not working resorting to hidden fields that I update with javascript..
Matt2012 29 Oct, 2014
Answer
Ok finally figured it all out.

I was forced into funny form fields by the way chronoforms makes you use user[email] for field name if you want to populate a field with data from your database.

It would make more sense to have a uniform approach that meant dynamic data could be put anywhere using {user[email]} in this case in the default value. Leaving field names to match the table field names.

I worked around this by using custom code in the onSubmit event before data save to add form values


$form->data['email'] = $form->data['UserData']['email'];
$form->data['name'] = $form->data['UserData']['name'];


I then renamed the MySQL fields to match this rather than rely on the autocreated one's matching the arrays (e.g. UserData[name] as table field).

I also found that Chronoforms is picky about ['UserData']['email'] and [UserData][email] the first works in custom code but the second is needed for defaulting db data into fields.

I also found other anomalies like this syntax is used in email message templates for dynamic data {UserData.name} and this UserData.email for dynamic to data without curly brackets.

In a way that's four different styles for dynamic data. When you're new that creates unnecessary complexity whilst debugging.

I know it sounds like im complaining. The thing is it's a great product amazing for free /cheap but a little bit of thought about dynamic data / emailing / saving to table could save a lot of pain and trial and error for us newbies.

I would like to suggest using {userdata.email} everywhere. No dynamic tab in email action just use curly brackets for dynamic data in email to fields / email message / custom code / form creation . Add default db data in the value field (not as a strange field naming structure) and name fields whatever you like and stick to {array.item} everywhere not array[item] or array['item'].

Lastly - clear the cache every time someone saves from within the form builder - that alone would have saved me a lot of pain!

Lastly many thanks for an otherwise great product.
Max_admin 29 Oct, 2014
Hi Matt,

Thanks for the post!

You can use curly brackets whenever you want to reference a field value, and if the value is in a sub array then you need the array path, hence: {UserData.email}

You have to use ['UserData']['email'] in PHP, that's how it works!

You better not use UserData[email] as a table field name, that may cause problems, if Chronoforms has created the fields in this format then this is a bug when auto generating the table fields, I will try to fix this.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
This topic is locked and no more replies can be posted.