In this thread, I wanted to ask can I put AJAX in the Twig Templates. Because I wanted to show data from databases in the interface as text. I have written the AJAX code below in the Twig Templates but seem there is no result in text.
HTML in Twig Template:
<p id="data"></p>
AJAX in Twig Template:
<script>
var ajax = new XMLHttpRequest();
var method = "GET";
var url = "data.php";
var asynchronous = true;
// receiving responses from data.php
ajax.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//converting JSON back to array
var data = JSON.parse(this.responseText);
console.log(data);
// html value for <tbody>
var html = "";
//looping through the data
for (var a = 0; a < data.length; a++)
{
var NRIC = data[a].NRIC;
//appending at html
html += "<p>" + NRIC + "<p>";
}
// replacing the <p>
document.getElementById("data").innerHTML = html;
}
};
</script>
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. -
Professional support
-
Plugins, theme and development
. I don't answer to private message.