Welcome, Guest
Username: Password: Remember me

TOPIC: Question condition with javascript

Question condition with javascript 1 year 2 months ago #95612

  • king_and1
  • king_and1's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Hello folks,

I´m back with a brand new question :D.

So i´ve been struggeling with an AJAX - call in Limesurvey. Now after i´ve made it , there´s a new problem coming up :/

Is it possible to set the condition for the next question to the answer of my ajax call ? I mean if the call works the next question will be shown. If theres an error the next question should not be shown.

Is this possible ?
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95617

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
It would probably be easiest to use the "success" setting of the AJAX call to toggle a hidden question. The hidden question could then be used for conditions/relevance for subsequent questions.

Can you attach your AJAX code and let us know what the possible returned data would be?
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95620

  • king_and1
  • king_and1's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
This is just a Test , so the code isn´t finished .

<script src="ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script>

function myCall() {
var request = $.ajax({
url: "ajax.php",
type: "GET",
dataType: "html"
});

request.done(function(jqXHR, textStatus) {
alert( "Request succeded " + textStatus );
});

request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95636

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
Okay, so depending on what the PHP script returns, the call could be something like this:
$.ajax({
	url: 'ajax.php',
	dataType: 'html',
	data: {var1: 1}, // Data passed to the PHP function
	success: function(results){
		if(results = 1) {
			$('#question123 input.radio:first').click();
		}
		else {
			$('#question123 input.radio:last').click();
		}
	},
	error: function(){
		alert('Fail!');
	}
});
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: king_and1

Question condition with javascript 1 year 2 months ago #95739

  • king_and1
  • king_and1's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Thanks for this reply, it´s very helpful. But how do i target the hidden Question ? For Example my hidden Question got the "QID 92" so should i just replace the "#question123" with the ID ?
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95744

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
"#question123" would become "#question92".

Or, you can do this:
var qID = 92;
 
$.ajax({
	url: 'ajax.php',
	dataType: 'html',
	data: {var1: 1}, // Data passed to the PHP function
	success: function(results){
		if(results = 1) {
			$('#question'+qID+' input.radio:first').click();
		}
		else {
			$('#question'+qID+' input.radio:last').click();
		}
	},
	error: function(){
		alert('Fail!');
	}
});
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95903

  • king_and1
  • king_and1's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Can you help me Change the succes function. So it´s not depending on the result it gets but only if it works or not. So if the Call works it just should answer the hiden question with the first radio input. And if not the second one.

I also would like to know if the code "input.radio:first" is complete ? Isn´t there somethink like Q_A1 or so ?

Hope you can help me another time ^^
Thx for youre help anyway
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95906

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
Can you help me Change the succes function. So it´s not depending on the result it gets but only if it works or not.
The success function gets fired if the request succeeds, so it will occur if the AJAX call found the PHP file, regardless of any results passed back from that file.

If you don't care about results, the call would look something like:
var qID = 92;
 
$.ajax({
	url: 'ajax.php',
	dataType: 'html',
	data: {var1: 1}, // Data passed to the PHP function
	success: function(results){
		$('#question'+qID+' input.radio:first').click();
	},
	error: function(){
		alert('Fail!');
	}
});

I also would like to know if the code "input.radio:first" is complete ?
Yes, the selector below is complete. It points to the first radio found in a question with an ID defined by "qID".
$('#question'+qID+' input.radio:first')
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95911

  • king_and1
  • king_and1's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
<script src="ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script>
function myCall() {
var request = $.ajax({
url: "http://10.1...../lime/ajax.php",
type: "GET",
dataType: "html"
data: {var1: 1},
});

request.done(function(results) {
$("question91 input.radio:first").click();
});

request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
};
</script><input onclick="myCall()" type="button" value="Check" />






Thats my code now , but nothink happens .... No error and no questions are showing up -.-'
The administrator has disabled public write access.

Question condition with javascript 1 year 2 months ago #95914

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
You have a couple of syntax errors in the AJAX call and are missing a "#" in the selector. Also, you do not need to call jQuery - it is already included with LimeSurvey.
<script type="text/javascript" charset="utf-8">
	function myCall() {
		var request = $.ajax({
			url: "http://10.1...../lime/ajax.php",
			type: "GET",
			dataType: "html",
			data: {var1: 1}
		});
 
		request.done(function(results) {
			$("#question91 input.radio:first").click();
		});
 
		request.fail(function(jqXHR, textStatus) {
			alert( "Request failed: " + textStatus );
		});
	};
</script>
 
<input onclick="myCall()" type="button" value="Check" />
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.127 seconds
Donation Image