Forums

Preload values in a form.

JeLu 05 Feb, 2014
I have asked about this before but that thread went over to another question. http://www.chronoengine.com/forums/posts/f26/t86981/populate-field-with-values-from-table-db.html

I want to service my users with some suggestions of values in the form. e.g. their name address etc. these values must be taken from their previous record. e.g. my user has posted 10 posts with some common values and some unique values. The common values may be the same in 9 times of 10 and then my form suggest these common values.

In CFv3 I have this feature with som simple coding.
<?php>
 $dbfo =& JFactory::getDBO(); 
 $query = "SELECT ftag FROM `#__chronoforms_my_form` WHERE cf_user_id = '$my->id' ORDER BY `cf_id` DESC LIMIT 1";
 $dbfo->setQuery($query);
 $foretag = $dbfo->loadResult();
 ?>

And in default value for the textbox I just use this.
<?php echo $foretag; ?>


It is simple and works with validation.

I tried to use DB Loader Action but it loaded every value and from the first record. If I put in ORDER BY in WHERE clause under Advanced Tab I get an error-message.

//JeLu
JeLu 05 Feb, 2014
Greyhead has suggested this code in previous mentioned post in this forum:
<?php
 $user =& JFactory::getUser();
 $db =& JFactory::getDBO();
 $query = "
     SELECT `columna`, `columnb`
         FROM `#__table_name`
         WHERE `cf_user_id` = '{$user->id}' ;
 ";
 $db->setQuery($query);
 $data = $db->loadArray();
 $form->data = array_merge($form->data, $data);
 ?>


I am not sure about what this code do? Is it just merge data with my form?

Should it be visible in input field? e.g in field with ID columna?

When I try this and check debuggercode, I can see that it keeps data but curly brackets does not work anymore.

What am I doing wrong?

//JeLu
JeLu 06 Feb, 2014
When I use this Common Code it empties the Array depending on the order of Actions. Something I forgot to mention is that, since it is a Multi Page form I have several Events.

//JeLu
JeLu 06 Feb, 2014
Do I have to put something in Field Default Value to see what´s prefilled?

//JeLu
JeLu 06 Feb, 2014
Is it possible to use something like
<?php echo $data->columna; ?>


Another question! In Custom Code Action I can choose "Controller" or "View", Which is the correct one?

I have tried to run this Action in OnLoad-event and in OnPageThree-event but not sure where it should be. Will the Custom Code "hang on" to the array all the way to Submit?

//JeLu
JeLu 06 Feb, 2014
I am NOT sure that I get any info merged to my Array!? This is what Debugger shows
Data Array: 
Array
(
    [chronoform] => Elevfil_grund
    [event] => Page Two
    [cf_sid] => 79028caabda24b01b3ec56891f678bbc
    [pnr] => 000000-1010
    [input_submit_14] => Nästa
    [d1a6b1e09327962fd688946edd290fc6] => 1
    [_FORM_FILES_] => Array
        (
        )

)

Validation Errors: 
Array
(
)

Shouldn´t I have the merged data in the Array too? I miss the value from the column I have in Custom Code SQL Query.

//JeLu
This topic is locked and no more replies can be posted.