This is, in essence, what AJAX is for. Your page loads, then javascript uses the XMLHttpRequest object to send a request to a server. After the server responds (presumably with output), a javascript event gives you a place to work with the output, including simply sticking it into the page like any other piece of HTML.
Here's a really basic request script to accomplish what you discuss in your example code:
The javascript
function getRequest() {
var req = false;
try{
// most browsers
req = new XMLHttpRequest();
} catch (e){
// IE
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
// try an older version
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
return false;
}
}
}
return req;
}
function getOutput() {
var ajax = getRequest();
ajax.onreadystatechange = function(){
if(ajax.readyState == 4){
document.getElementById('output').innerHTML = ajax.responseText;
}
}
ajax.open("GET", "do_query.php", true);
ajax.send(null);
}
The HTML
<a href="#" style="display:block; color:#000033; font-family:Tahoma; font-size:12px;"
onclick="getOutput(); return false;"> test </a>
<span id="output"></span>
The PHP
// file do_query.php
<?php
echo query('hello!');
?>
For further reading, check out the two documents I linked to above, and check out these tutorials: http://www.tizag.com/ajaxTutorial/ajaxxmlhttprequest.php
<?php query("hello"); ?>
rendered by php when page is loaded? if yes then he can get output ofquery("hello")
in js function. – TheVillageIdiot Aug 23 '11 at 18:00