Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Add php or javascript code to display a http requested text.

  • jjesusfilho
  • jjesusfilho's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 3 weeks ago - 7 years 3 weeks ago #159506 by jjesusfilho
I have a html page with several paragraphs. I would like to add a new of these paragraphs every time someone starts a survey. Can I add a php code to a question to request these paragraphs based on the the number of the survey. I was thinking of something like this:

$html = file_get_html(' example.com ');

$body = $html->find('p[form_number]');
Last edit: 7 years 3 weeks ago by jjesusfilho.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 3 weeks ago - 7 years 3 weeks ago #159508 by tpartner
You cannot insert PHP into the question but, if the document to be loaded resides on the same server, you can use the jQuery load() method.

Something like this will insert the contents of an element with ID "remoteContentToLoad" from example.com into an element with ID "elementToBeLoaded" in your survey page.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:complete',function() {
 
    $('#elementToBeLoaded').load('https://example.com #remoteContentToLoad');
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 3 weeks ago by tpartner.
The following user(s) said Thank You: DenisChenu, jjesusfilho
The topic has been locked.
  • jjesusfilho
  • jjesusfilho's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 3 weeks ago - 7 years 3 weeks ago #159523 by jjesusfilho
Hi @tpartner, thank you very much for the helpfull reply. Can I ask you a further question? What is the elementToBeLoaded, a html div element? How about the remoteContentToLoad?
If I understood right, I should do something like that:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:complete',function() {

$('#div1').load(' example.com #text');
});
</script>
But this is not working, which means I did not understand. Sorry I am not familiar with jquery load.
Last edit: 7 years 3 weeks ago by jjesusfilho.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 3 weeks ago - 7 years 3 weeks ago #159537 by tpartner
The "elementToBeLoaded" is the ID of an element in your survey where you want to insert the dynamic text. So, assuming you want the dynamic text in a question, the source of that question might look something like this:

Code:
<!-- The static text -->
<p>This is some static question text...</p>
 
<!--  A container for the dynamic text -->
<div id="elementToBeLoaded"></div>
 
<!-- A script to insert the dynamic text -->
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:complete',function() { 
    $('#elementToBeLoaded').load('https://example.com #remoteContentToLoad');
  });
</script>


The "remoteContentToLoad" is the ID of an element in the remote HTML page that contains the text to be loaded. So, the source of the remote page (example.com) might look something like this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Dynamic Text</title>
    </head>
 
    <body>
        <div id="remoteContentToLoad">
            This is the dynamic text that is to be loaded into the survey...
        </div>
    </body>
</html>


Also remember that the remote HTML page must reside on the same server as the survey.

More on the jQuery load() method - api.jquery.com/load/

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 3 weeks ago by tpartner.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose