Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Accessing a question variable (freeform) on same page after forward/backward

Hi all,

is it possible to access the last value (if answered) of the the current question from within perl script?

I need to output different HTML-Code in a free-form question based on the save variable state of the question. Example:

If the user filled the variable "q2_year" (input type text) on Q2 and moves on to Q3 and then navigates back to Q2 I want to modify the free-form content in Q2 based on the "q2_year" variable  the user specified on an earlier Q2 visit.

GETVALUE in unverified Perl Script doesn't seem to work:


[% Begin Unverified Perl
    
    my $str = "";
    my $year = GETVALUE("q2_year");
    #
    # I know that this code runs on the server but I want to display the q2_year input-field only if the user already
    # filled out the form (e.g. navigating back to this question from q3)
    # 
    # I know that the "code" is extra verbose and not optimized :P
    # 
    if (GETVALUE("q2_radio") eq "1" && $year ne "") {
        $str = qq(<input name="q2_year" id="q2_year" type="text" size="20" value="$year">);
    } else {
        $str = qq(<input name="q2_year" id="q2_year" type="text" size="20" value="" style="display: none;">);
    }
    
    return $str;

End Unverified %]

$ra is always empty :-(.

Any ideas on what's wrong?  

Thanks in advance!
asked Jul 22, 2020 by anonymous
edited Jul 22, 2020
Can you please let me know in detail what question names and type you have created and what text you want in the ff variable.
You can give example for easy understanding.
I've updated the question ... is it clearer?

Thanks!
You don't need Sawtooth Script for this; JavaScript is up to the task.  Here is a simple example:

<span id="specialSpan"></span>

<script>
$(document).ready(function(){
    var resp = SSI_GetValue('[% QuestionName() %]_year');
    if (resp) {
        $('#specialSpan').text('response = ' + resp);
    }
})
</script>
You can not get a value of a variable on same page using perl. You can do this using JavaScript:

You can use below, similar to what Zachary mentioned, only variation is getting the value of "q2_year":

<span class="oldvar"></span>
<script>
$(document).ready(function(){
    var oldvalue=$("#q2_year").val();
    $(".oldvar").text(oldvalue);
});
</script>

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...