In reference of my other question: Context-aware AJAX call in a modular site , is it possible to set up an AJAX proxy in a convenient way?
I'd wish to make AJAX requests with dynamic url without tainting the JavaScript code with server-side PHP instructions, computing the right path for the ajax php file in a modular Apache/PHP/MySQL site, a rough CMS written by me. In other words, the site has plugins and components with their own folder structures containing their CSS, JS, and PHP and I have functions to retrieve their folder dynamically. I'd wish that:
$("#mydiv").load(siteRoot + "/ajax.php?plugin=datagrid&
action=myaction&... other params...");
will instead call (with the URL computed server-side by PHP):
{siteRoot}/components/datagrid/ajax/myaction.php?... other params ...
(obviously with all the possible checks against injections, CSRF and other hacks).
/ajax/get.php file should look something like this:
$plugin = $_GET['plugin'];
$action = $_GET['action'];
$get = array();
foreach ($_GET as $k=>$v) {
if($k!='plugin' && $k!='action') {
$get[] = "{$k}={$v}";
}
}
$url = 'siteRoot/components/'.$plugin.'/ajax/'.$action.'.php?'.implode('&', $get);
header("Location: {$url}");
Of course, you need to add some security checks to above code
EDIT: The downside is that it won't work for sending POST requests
Instead you can also declare your javascript variable in your first page request and then use it let say SITE_URL contains your php computed url
var siteRoot = '<?php echo SITE_ROOT?>' // or some thing else
so for any .js script included after this line will have defined siteRoot var.