Hello,
I have written a php file and saved it in the folder .../assets/snippets/mysnippet/mysnippet.php
and written a chunk that contains the javascript code for ajax call:
<script type="text/javascript">
document.getElementById(’some_tag’).someevent = function () {
var id = document.getElementById(’select_district_tag’).value;
var url = ’someaddress/index-ajax.php’; //-- I have changed
var pars = ’q=assets/snippets/mysnippet/mysnippet.php’;
new Ajax.Request(url, {method: ’get’, parameters: pars, onComplete: showResponse});
};
function showResponse(request)
{
document.getElementById(’links’).innerHTML = request.responseText;
};
</script>
This is the code of php:
<?php
include_once("...someurl/manager/includes/document.parser.class.inc.php");
//$modx = new DocumentParser;
//print_r($modx);
//$arr = $modx->getChildIds(1, 1);
echo "HELLO";
?>
Ajax works if I just write echo HELLO. It is returned well and appear on the page. But if uncomment the line with modx, for example - $modx = new DocumentParser, it doesnt return anything. Firebug console shows empty response.
Why this is so?
I use Modx version 0.9
Thank you
-
☆ A M B ☆
- 24,524 Posts
A much better way to do this is to create a snippet with your PHP code, create a resource with no template and only the snippet tags, then use the URL to that resource as your AJAX request URL. This way all of the MODx API and arrays are available to your PHP code. No need to include anything.
I often use the eForm snippet just as it is for this; all I need in the HTML is a container for my AJAX to load the eForm output into.
Thank you,
But how can I get the GET parameter in the resource(html-document)? Is it right that when I type the url of document, the cms takes it and using the sql request extracts a page with that url from the database? So the cvs can not parse the arguments after "html" in such a request "somepage.html?someid=456"
Thank you
-
☆ A M B ☆
- 24,524 Posts
It will be there. Your processing snippet will have the code to gather the GET, POST or REQUEST (whichever you prefer to use) and process it, echoing or returning whatever is appropriate for the AJAX response.