So,
Step 1: Create a TV called "snippet"
Step 2: Create a template called for example "Ajax Snippet Call" and enter the TV. In our case [*snippet*]
Step 3: Create a document, assign it the above template and enter the snippet you want to run in the "snippet" TV. Lets say "[[myAjaxSnippet]]". You can also assign parameters to it. [[myAjaxSnippet &fromPage=`A`]]
Step 4: create your snippet.
Lets do one
<?php
if($fromPage == 'A'){
echo $fromPage;
}else{
echo "Something Else";
}
?>
call the page from javascript. You can also use the post method to send values to the page(snippet)
<?php
if($_POST['fromJs'] == 'A'){
echo $_POST['fromJs'];
}else{
echo "Not A";
}
?>
Do not forget to clean your post values in case you want to use them in the database.
PS: Not tested but it should work