surveyID, GroupID, QuestionID and others in ajax/PHP call

More
3 years 2 months ago #112123 by mayeulk01
mayeulk01 created the topic: surveyID, GroupID, QuestionID and others in ajax/PHP call
Hello,
For keywords in the template files, there is nothing for the SurveyID (the long number as 762974). Also, the groupid can be inferred from the SGQ code such as 762974X13X51, but the SGQ code is not working in startgroup.pstpl
Are there other ways to get other variables than the one sent in AJAX call from a Limesurvey Keyword in a template? Including the token, the group id (within startgroup.pstpl)?

Below is how I try to get some of these parameters with AJAX interaction (not working yet though), using some regular expression

Within question.pstpl:
<div id="question_history_ajax_script">
    <script type="text/javascript" charset="utf-8">
 
    // Identify the current script element NOTE: must be before $(document).ready(function()
    var scripts = document.getElementsByTagName('script');
    var thisScript = scripts[scripts.length - 1];
 
    $(document).ready(function() {
 
        // Identify the current script's parent element
        var scriptParent = $(thisScript).parent();
 
        // Pull in the remote file
        $.ajax({
            type: "GET",
            url: '{TEMPLATEURL}lime_question_history.php',
            data: 'country={TOKEN:ATTRIBUTE_8}, role={TOKEN:ATTRIBUTE_1},  sqg={SGQ}',
            success: function(results){
                // Load the remote file contents into the script parent element
                $(scriptParent).append(results);
            }
        });
    });
 
</script>
</div>

<?php
$sgq = $_GET['sqg'];
 
if(preg_match('(\d+)X(\d+)X(\d+)',$str,$matches)) {
   $surveyid = $matches[1];
   $groupid = $matches[2];
   $question = $matches[3];
}
?>

Note, again, that I would like some of this in the EndGroup group template too.

Pointers in the manual, forum (or if absolutely needed source code in GitHub) would be appreciated.
Thank you!
Cheers,
Mayeul

Please Log in to join the conversation.

More
3 years 2 months ago - 3 years 2 months ago #112127 by Ben_V
Ben_V replied the topic: surveyID, GroupID, QuestionID and others in ajax/PHP call
Hi,
the most of the time the following {keywords} do the trick:

Survey ID: {SID}
Group ID: {GID}
Question ID: {QID}

(Old "SGQ code such as 762974X13X51" can be retrieved using {SID}X{GID}X{QID} syntax or {SGQ} placeholder )

To go further or check if a {keyword} is allowed in a specific template file, you can have a look at EM Roadmap
.

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last Edit: 3 years 2 months ago by Ben_V.
The following user(s) said Thank You: tomscher, mayeulk01

Please Log in to join the conversation.

More
3 years 2 months ago #112154 by Mazi
Mazi replied the topic: surveyID, GroupID, QuestionID and others in ajax/PHP call
If some of the above placeholders don't work at certain template files, you can try to make use of some of the hidden INPUT elements Limesurvey uses.

When checking the source code of any question page you should find elements like:

<input type="hidden" name="fieldnames" value="157348X205X1087|157348X205X1037|157348X205X1038" id="fieldnames">

<input type="hidden" name="sid" value="157348" id="sid">

<input type='hidden' name='lastgroup' value='157348X205' id='lastgroup' />

<input type="hidden" name="token" value="12345" id="token">

Especially the first one should help you dealing with any question on the current page.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"
The following user(s) said Thank You: mayeulk01

Please Log in to join the conversation.

More
3 years 2 months ago - 3 years 2 months ago #112157 by mayeulk01
mayeulk01 replied the topic: surveyID, GroupID, QuestionID and others in ajax/PHP call
Thank you both! This works well:
<div id="show_group_history_ajax_script">
    <script type="text/javascript" charset="utf-8">
    // Identify the current script element NOTE: must be before $(document).ready(function()
    var scripts = document.getElementsByTagName('script');
    var thisScript = scripts[scripts.length - 1];
    $(document).ready(function() {
        // Identify the current script's parent element
        var scriptParent = $(thisScript).parent();
        // Pull in the remote file
        $.ajax({
            type: "POST",
            url: '{TEMPLATEURL}show_group_history.php',
            data: 'country={TOKEN:ATTRIBUTE_8}&role={TOKEN:ATTRIBUTE_1}&token={TOKEN}&surveyid={SID}&groupid={GID}',
            error: function(xhr, status, error) { // http://stackoverflow.com/questions/1637019/how-to-get-the-jquery-ajax-error-response-text
	      var err = eval("(" + xhr.responseText + ")");
	      alert(err.Message);
	    },
            success: function(results){
                // Load the remote file contents into the script parent element
                $(scriptParent).append(results);
            }
        });
    });
 
</script>
</div>
Mayeul
Last Edit: 3 years 2 months ago by mayeulk01. Reason: better code display

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!