• Resolved gunnilstam

    (@gunnilstam)


    Hi again,

    I am having some difficulty pre-populating a field with the ‘last value that the same user entered’. Example: user 1-3 record blood pressure every day, but each day the number in the form is defaulted to the value from yesterday.

    I thought I would achieve this by using
    1) a Number DS with an sql query:
    [select bp from labs where id = (select max(id) from labs where user_id = @user_id)],
    and then
    2) default the Blood Pressure field to the Number DS.

    however, I have three problems:

    1 – my query resolves, but only without hardcoded ‘1’ or ‘2’ replacing the @user_id part – i do not have the correct syntax to filter on user_id – how could I use the ‘current session user id’ in my SQL query?

    2 – my DB connection is good and my query resolves (by testing it in the Form Builder, I get the popup with a result), but I still cannot see the result in the number DS when i preview the whole form. What is going wrong here?

    3 – what would the correct code in the Blood Pressure field be, to default it to the Number DS?

    Do you agree with the approach? If so i would very much appreciate feedback on the above.

    Best Regards,
    Lars

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @gunnilstam,

    First, in the “SELECT” clause you should use an alias (as value) indicating to the plugin the column used for filling the field.

    Second, the constant that represents the id of logged user is: {user.id}

    So, the query would be:

    
    select max(id) as value from labs where user_id = {user.id}
    

    Please, visit the following link with additional details:

    https://cff.dwbooster.com/documentation#constants-in-queries

    Best regards.

    Thread Starter gunnilstam

    (@gunnilstam)

    perfect, thank you. the query works.

    i did not, however, succeed in defaulting field 3 with either a number DS or a Hidden DS.

    the code i added is intended to set 3 (slider) to the value of 35 (DS)

    [CP_CALCULATED_FIELDS id=”11″]
    <SCRIPT>
    cpcff_default = { 1 : {} };
    cpcff_default[1][ ‘fieldname3’ ] = ‘<%fieldname35_value%>’;
    </SCRIPT>

    do you have a better approach to this one?

    much appreciated,
    Lars

    Plugin Author codepeople

    (@codepeople)

    Hello Lars,

    If the form 11 was inserted in the thank you page of another form, and you want to pre-fill the fieldname3 with the value entered into fiedname35 field of the submitted form, you should use in the thank you page both shortcodes:

    – The shortcode to insert the form: [CP_CALCULATED_FIELDS id="11"]

    – And the shortcode for the summary, because you need to replace the special tag <%fieldname35_value%> with the corresponding value. As follows:

    
    [CP_CALCULATED_FIELDS id="11"]
    [CP_CALCULATED_FIELDS_RESULT]<SCRIPT>cpcff_default = { 1 : {} };cpcff_default[1]['fieldname3'] = '<%fieldname35_value%>';</SCRIPT>[/CP_CALCULATED_FIELDS_RESULT]
    

    Note that I’ve inserted all javascript code into only one line, some plugins replace the changes of line with p tags, and a p tag into a block of javascript code provokes a fatal error.

    Pay attention, the [CP_CALCULATED_FIELDS_RESULT] shortcode is only available in the Professional, Developer and Platinum versions of the plugin, so, I’ve assumed in the previous code you are using one of them.

    Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pre-populate with last entry’ is closed to new replies.