Time for the 2023 version of our favorite question - how to implement select2 with ajax data in Qualtrics!
From Qualtrics, I need to pass the study site selected on the form to my API and get back a list of study participants to put into a select so the desired participant can be selected. What is the proper way to do this?
I tried making the API call in the Survey Flow, setting an embedded data variable for the data coming back, and pointing the select2 ajax data at it but that is not working. The API is sending the data in the correct select2 format (https://select2.org/data-sources/arrays). I can see it in Postman. But nothing is showing in Qualtrics.
I also thought I could call the API from the select2 ajax URL but I don’t know the syntax for sending the Qualtrics parameters to it on the URL so bailed on that attempt and came here.
I followed this 4 year old article for the basic setup so I have the correct header css/js, question type, and question JavaScript. Qualtrics.SurveyEngine.addOnReady(function() { jQuery("#"+this.questionId+" select").select2(); });
I feel like I’ve tried everything but the pieces aren’t coming together. I’m happy to Zoom with anyone who has done this before and document the exact steps to share with others! Thank you in advance!
Page 1 / 1
@ajemerson
Unsure about the approach suggested above, but can suggest an alternative.
Your issue can be resolved, if you can bring the entire data within Qualtrics and use Supplemental Data to have the dropdown list.
Hope this helps!
P.S.: it’s a paid feature in Qualtrics.
@ajemerson,
The proper way to do it is to use the select2 ajax option. Use the ajax data option to set the parameters that your server side script will use. For example:
params.term is the search term (what the respondent entered).
Thank you for responding, @Deepak. The list of study participants that I need to pull back into Qualtrics changes and is not static. I think I need to go with the ajax call on this.
And thank you, @TomG, for responding. I need to pass an API key in the ajax call. A cursory look at the select2 documentation didn’t have an example of that. Do you have one?
How do I pass the site selected into params.term? It’s a question on the survey. I set an embedded data variable to make it easier to reference - ${e://Field/site}. I don’t imagine params.term = ${e://Field/site} would work, would it?
Stackoverflow says I can do this for the header:
ajax: { beforeSend: function (request) { request.setRequestHeader("X-CSRF-TOKEN", csrf_token); }, }
I remain interested in how to send the form field to the ajax call.
I’m getting there! Now have to deal with Cross-Origin Request block.
var site = Qualtrics.SurveyEngine.getEmbeddedData('site');
jQuery("#"+this.questionId+" select").select2({
ajax: {
url: 'myUrl',
dataType: 'json',
headers: {'Authorization': 'Key myKey'},
cache: true,
data: function(params) {
return {
site: site,
term: params.term || '',
page: params.page || 1,
pageSize: 20
}
}
}
});
And thank you, @TomG, for responding. I need to pass an API key in the ajax call. A cursory look at the select2 documentation didn’t have an example of that. Do you have one?
How do I pass the site selected into params.term? It’s a question on the survey. I set an embedded data variable to make it easier to reference - ${e://Field/site}. I don’t imagine params.term = ${e://Field/site} would work, would it?
How is the API key passed? As a url parameter or in the header?
params.term is meant to be the contents of the select2 search box. I haven’t tried to set it. Maybe set the value of the search box with JS and trigger an input event?
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://myUrl/participants.php?site=WRJ&term=&page=1&pageSize=20. (Reason: header ‘authorization’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response).
You can send other parameters to your web service by including them in the data function return:
Your web service must send Access-Control-Allow-Origin in the header.
BTW, your web service must also return data in the format prescribed by select2: an array of objects with ‘id’ and ‘text’ for each option and a pagination flag if applicable.
Thank you so much for your guidance on this, @TomG ! That is what I shall tackle today. Postman shows my data coming back like this:
Cross-Origin error - The API is PHP. I added this to the code which resolved the error: header('Access-Control-Allow-Headers: Authorization, Content-Type, key');
header('Access-Control-Allow-Origin: *');
I also had to fix the key. Here is the Qualtrics JavaScript I used:
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var site = Qualtrics.SurveyEngine.getEmbeddedData('site');
jQuery("#"+this.questionId+" select").select2({
theme: "classic",
width: "200px",
placeholder: "Select participant...",
ajax: {
url: 'https://myUrl/participants.php',
dataType: 'json',
headers: {'key': 'myKey'},
cache: true,
data: function(params) {
return {
site: site,
page: params.page || 1,
pageSize: 20
}
}
}
});
});
End result = hooray!!
Thanks again, @TomG , for helping!!
@TomG- One last question… how do I save what was selected in the select2 so I can use it on the form? I’d like to set it as embedded data. In the Survey Flow, I tried to add a Set Embedded Data block after the select2 question, but the question isn’t showing up in Insert Piped Text > Survey Question so I can’t pick “Selected Choices”.
I’m guessing in JavaScript I can Qualtrics.SurveyEngine.setEmbeddedData('participant_study_id') but I don’t know what to set it to or where/when to set it.
Do I add this to the addOnUnload function - Qualtrics.SurveyEngine.setEmbeddedData('participant_study_id') = jQuery("#"+this.questionId+" select").filter(":selected").text()
@TomG- One last question… how do I save what was selected in the select2 so I can use it on the form? I’d like to set it as embedded data. In the Survey Flow, I tried to add a Set Embedded Data block after the select2 question, but the question isn’t showing up in Insert Piped Text > Survey Question so I can’t pick “Selected Choices”.
I’m guessing in JavaScript I can Qualtrics.SurveyEngine.setEmbeddedData('participant_study_id') but I don’t know what to set it to or where/when to set it.
Do I add this to the addOnUnload function - Qualtrics.SurveyEngine.setEmbeddedData('participant_study_id') = jQuery("#"+this.questionId+" select").filter(":selected").text()
That did the trick! Thank you so much, @TomG . We figured out a lot here. I hope others find it helpful. The exact code I used for the last piece is below.
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var selected_participant = jQuery("#"+this.questionId+" select option:selected").text();