Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Trying to using Ajax

Trying to using Ajax 2 years 6 months ago #71435

  • jmcos
  • jmcos's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
I am trying to call an Ajax function and then pass a reply to a PHP routine.
I have the following, but the alerts never appear. If I add an additional <script> section for Javascript at the beginning with an alert, then that alert appears. Is what I am trying to do possible?
<script language="javascript" type="text/javascript">
 alert("Here");
<!-- 
//Browser Support Code
 
function ajaxFunction(){
   var ajaxRequest;  // The variable that makes Ajax possible!
 
   try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
   } catch (e){
	// Internet Explorer Browsers
	try{
	   ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	   try{
	      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
	   } catch (e){
		// Something went wrong
		alert("Unsupported browser");
		return false;
	   }
	}
   }
//
// Create a function that will receive data sent from the server
//
        var url = "{INSERTANS:14291X67X597}";
        var params="url="+url;
 alert(params);
        ajaxRequest.open("POST", "verifyURL.php", true);
 
//Send the proper header information along with the request
        ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxRequest.setRequestHeader("Content-length", params.length);
        ajaxRequest.setRequestHeader("Connection", "close");
 
        ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
	if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
		alert(ajaxRequest.responseText);
	}
}
        ajaxRequest.send(params);
      }
   }
}
//-->
</script>
<form name="urlForm" onload="ajaxFunction()" type="hidden">
</form>
Last Edit: 2 years 6 months ago by DenisChenu. Reason: Please use [code]
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #71452

  • DenisChenu
  • DenisChenu's Avatar
  • NOW ONLINE
  • Moderator Lime
  • Posts: 6222
  • Thank you received: 791
  • Karma: 238
Hello,

LimeSurvey implement jquery, and jquery is great !! :woohoo:

For ajax:
api.jquery.com/category/ajax/

For alert:
$(document).ready
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #71581

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5301
  • Thank you received: 291
  • Karma: 247
Can you give us a little background information?

- Which Limesurvey version do you use?
- Which template do you use?
- Where did you add the code?
- What do you want to achieve with this Ajax call?

Have a look at docs.limesurvey.org/Workarounds%3A+Manip...wers_for_text_inputs for a (different) Ajax call example.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #71890

  • jmcos
  • jmcos's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
The version is 1.91 (build 11379).

After additional research, I am using Jquery and Ajax. I am trying to take an entered URL (in its simplest format, i.e., www.somthing.com) and ping it to see if it exists.
Right now I have

</script><script type="text/javascript">

$(document).ready(function () {
var url = "{INSERTANS:14291X67X597}";

$.post("verifyURL.php", {verURL: url, async: false});
});

</script>

The value of url appears correctly, but I am not sure of the Jquery post logic. I have the following lines in my PHP file:

$aURL = htmlentities($_POST);
echo $aURL;

But I do not see anything echoed.
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #71910

  • DenisChenu
  • DenisChenu's Avatar
  • NOW ONLINE
  • Moderator Lime
  • Posts: 6222
  • Thank you received: 791
  • Karma: 238
Hello,

You have to put some element for return, look at jquery example:
$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
     alert("Data Loaded: " + data);
   });

Denis
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #71927

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4069
  • Thank you received: 746
  • Karma: 341
I would handle this with a listener on the text input. When a change to the input is detected, send the entered value to the PHP script to check if it can be opened.

The script in LimeSurvey (replace "QQ" with the short-text question ID:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// The question ID
		var qID = QQ;
 
		// A listener on the text input
		$('#question'+qID+' input.text').change(function(){
 
			// Get the text input value
			var urlToTest = $(this).val();
 
			// Query the remote PHP file with the text input value
			$.post('path/to/your/phpFile.php', {verURL: urlToTest}, function(data) {
				// Clean up the returned data
				data = data.replace(/[\s]/g,'');
 
				if(data == 'okay') {
					alert ('The URL is valid.');
				}
				else {
					alert ('The URL is NOT valid');
				}
			});
		});
 
	});
 
</script>

And in the PHP file:
<?php
 
	// The URL passed from LimeSurvey in the POST array
	$aURL = htmlentities($_POST['verURL']);
 
	// Check to see if the file exists by trying to open it for read only
	if (fopen($aURL, "r")) {	
		echo 'okay';	
	}
 
?>
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.

Re: Trying to using Ajax 2 years 6 months ago #71955

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
Longer-term, we could support this via enhanced EM-based validations.

We could add a validate_url() function to EM that does the validation you're seeking. Then, we could enhance LimeSurvey validation to support any EM function, not just regular expressions. That way, as soon as you change the value of the field, it will set the background color of the field to red if it fails any of the validations.

This sort of change to EM only takes a few minutes once we have a good PHP function and javascript equivalent.

/Tom
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #72920

  • jmcos
  • jmcos's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
I have added a return function, but still nothing.

var url = "{INSERTANS:14291X67X597}";

$.post('verifyURL.php', {verURL: url, async: false},
function(result){
if (result == "invalid") (
alert("Invalid URL. Please re-enter.");
} else {
alert("Valid URL. Please continue.");
}
});

Is there any way to determine if I am entering the PHP code other than including the following?

$aURL = htmlentities($_POST);
echo $aURL;
The administrator has disabled public write access.

Re: Trying to using Ajax 2 years 6 months ago #72922

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4069
  • Thank you received: 746
  • Karma: 341
Why are you running it asynch?

Did you try with my code?
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.

Re: Trying to using Ajax 2 years 6 months ago #72936

  • jmcos
  • jmcos's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
Yes. Same results. Maybe I'm missing something in the implementation. Let me explain the setup further. Right now, I have 2 questions. In Q1, the user enters something like www.google.com. Then, in Q2, there's a question, but this one exists solely so I can reference the entry in Q1, since I cannot reference it otherwise. When I advance to Q2 (which is also in another group), I expect the jQuery call to call the PHP code and determine if the website exists.

Will the field listener be able to help? It seems that that code would have to be in Q1. But, if so, then I can't reference the SGQA indentifier.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.303 seconds
Donation Image