data['campoDynamicDropqown']; ?>"> dropdown dynamic data in database - Forums

Forums

dropdown dynamic data in database

agostino87 28 Nov, 2016
Hello, I have 4 dynamic dropdown linked together (the same as the common province region), it works well, the problem is: to save in the database saves the id for the value and not the correct value.
the tables are in connection with the id.
how do I fix?

I tried this solution but it does not work (I copy the value from the dropdown in a text field, but also saves the id and not the value. The code is tested this:
1) Create a hidden field in your form for campoDaSalvare
   2) add a "Custom Code" action to the "On Submit" event before your "DB Save Action"

In the Custom Code action insert PHP code to set the campoDaSalvare field to the appropriate value depending on the selected drop down value.
In ChronoForms v4 or v5, this will be something similar to this:

<?php $form->data['campoDaSalvare'] = $form->data['campoDynamicDropqown']; ?>
GreyHead 28 Nov, 2016
Hi agostino87,

If you save the id then you can always look up the value when it is needed, that is generally better practice for database design as if you later change the value then you will automatically get the new one.

I assume that you need the IDs to do the later look-ups - otherwise you could return the value instead of the ID.

If that is so then the best solution is probably to add Custom Code in the final step to look up the IDs on the database tables and add the matching values to the $form->data array.

I can’t tell what the code you posted is supposed to do :-(

Bob
agostino87 28 Nov, 2016
the value is displayed correctly in the dropdown, but when it is saved in the database saves the id😟

I hope someone has the solution
GreyHead 28 Nov, 2016
Hi agostino87,

A drop-down option has two parts - the value which is saved and the text which is displayed.

Please see my previous post.

Bob
agostino87 28 Nov, 2016
yes, this in the first dropdown in the tab "dynamic data", I put the value of the field is in the "text" that "value" so displays and saves the correct value in the database. The problem is for the remaining three dropdown of the valued rescue.
But being the remaining dropdown connected to the first value chosen by relazxione between tables, not imposed the tab "dynamic data."

example

http://www.icagenda.it/multiple-select-dropdown-list-with-ajax.html
GreyHead 28 Nov, 2016
Hi agostino87,

Please see my earlier post.

Bob
agostino87 28 Nov, 2016
https://www.chronoengine.com/forums/posts/f2/t96791.html

many topic like this speak of the argument, however, in no one explains how to save in the database the correct value and not the id
GreyHead 28 Nov, 2016
Hi agostino87,

As I posted earlier If that is so then the best solution is probably to add Custom Code in the final step to look up the IDs on the database tables and add the matching values to the $form->data array.

Bob
agostino87 28 Nov, 2016
yes, this is a solution. I try to do some research
agostino87 29 Nov, 2016
unfortunately I can not find a similar case and a possible solution
GreyHead 29 Nov, 2016
Hi agostino87,

It is not complicated. You have the IDs - you need to add a Custom Code action that looks up the ID in the database and gets the value.
<?php
$db = \JFactory::getDBO();
$query = "
    SELECT `col_name`
        FROM `#__table_name`
        WHERE `id` = '{$form->data['input_name']}' ;
";
$db->setQuery($query);
$form->data['some_name'] = $db->loadResult();
?>

Bob
agostino87 29 Nov, 2016
1 Likes
great, it works perfectly now. thank you so much
agostino87 05 Dec, 2016
hello, the code works fine, I was able to add in many parts of the form. If at the same time I would like to see the value that except db even in a readonly textbox how can I do ??
GreyHead 05 Dec, 2016
Hi agostino87,

I think that something got lost in translation :-(

You can set an input element to readonly by adding readonly=readonly to the Extra Parameters box - does that help?

Bob
agostino87 06 Dec, 2016
I'm sorry for my English school

Yes, for the read-only field use "readonly"

    <?php
    $db = \JFactory::getDBO();
    $query = "
        SELECT `col_name`
            FROM `#__table_name`
            WHERE `id` = '{$form->data['input_name']}' ;
    ";
    $db->setQuery($query);
    $form->data['some_name'] = $db->loadResult();
    ?>


with this code save the value and not the id, in addition to this I would like to see the value in a textbox, how can I do?
GreyHead 06 Dec, 2016
Hi agostino87,

Sorry but I still don't know what your question is - that code reads from the database, it doesn't save anything.

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