- Posts: 9
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Test questions / question groups based on token attributes
- michaelve
- Topic Author
- Offline
- New Member
Less
More
9 years 2 months ago - 9 years 2 months ago #117144
by michaelve
Test questions / question groups based on token attributes was created by michaelve
In several questions we use the token attributes of the respondent in the question's javascript. For instance:
--javascript being passed to php
var tokenAttribute1 = {TOKEN:ATTRIBUTE_1};
--php script
$sql = "select * from foo WHERE attr='".$tokenAttribute1."'" ;
While testing these questions (lay-out etc) however, we would like some default value for the tokenAttribute1 without going through the survey each time we wish to see some changes. As the {TOKEN:ATTRIBUTE_1} is not set or defined when testing a single question, javascript throws a syntax error. I was wondering whether there is solution along the lines of this topic in the bugtracker: where a isset function is proposed. I think the expression manager passes an object or variable back after parsing the attribute, but I do not know how to evaluate this.
Psuedo code would be:
tokenAttribute1 = 0;
if(isset{TOKEN:ATTRIBUTE_1})
{
tokenAttribute1={TOKEN:ATTRIBUTE_1};
}
A similar test could be used in questions relying on answers from previous question - a bit like this topic
Thanks in advance!
--javascript being passed to php
var tokenAttribute1 = {TOKEN:ATTRIBUTE_1};
--php script
$sql = "select * from foo WHERE attr='".$tokenAttribute1."'" ;
While testing these questions (lay-out etc) however, we would like some default value for the tokenAttribute1 without going through the survey each time we wish to see some changes. As the {TOKEN:ATTRIBUTE_1} is not set or defined when testing a single question, javascript throws a syntax error. I was wondering whether there is solution along the lines of this topic in the bugtracker: where a isset function is proposed. I think the expression manager passes an object or variable back after parsing the attribute, but I do not know how to evaluate this.
Psuedo code would be:
tokenAttribute1 = 0;
if(isset{TOKEN:ATTRIBUTE_1})
{
tokenAttribute1={TOKEN:ATTRIBUTE_1};
}
A similar test could be used in questions relying on answers from previous question - a bit like this topic
Thanks in advance!
Last edit: 9 years 2 months ago by michaelve. Reason: Change in variable name
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10114
- Thank you received: 3595
9 years 2 months ago - 9 years 2 months ago #117154
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Test questions / question groups based on token attributes
The problem is that Expression Manager wraps the variable in several span elements if it is undefined. These span elements will cause JavaScript errors unless enclosed in quotes.
So try something like this to prevent those errors and detect the span elements:
So try something like this to prevent those errors and detect the span elements:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var tokenAttribute1 = "{TOKEN:ATTRIBUTE_1}"; if(tokenAttribute1.indexOf('<span') >= 0) { tokenAttribute1 = 0; } }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 2 months ago by tpartner.
The following user(s) said Thank You: michaelve
The topic has been locked.
- michaelve
- Topic Author
- Offline
- New Member
Less
More
- Posts: 9
- Thank you received: 0
9 years 2 months ago #117160
by michaelve
Replied by michaelve on topic Test questions / question groups based on token attributes
Great, thanks, it works! The quotes indeed appear in the generated webpage. Instead of checking for the span, testing for the length works as well.
Cheers, Michael
Code:
var tokenAttribute1 = "{TOKEN:ATTRIBUTE_1}"; if(tokenAttribute1.length <= 0) { tokenAttribute1 = 0; }
Cheers, Michael
The topic has been locked.