Database maintenance tools (like TheUsual) often offer dropdown choices of databases and tables, then various possible ways of changing or deleting those databases and tables, or creating others.
That is, the user can easily do something to make that dropdown content obsolete. It's not practical to repaint the whole page every time this happens.
Enter Asynchronous Javacsript And XML, aka AJAX —a set of methods for asynchronously sending data to a server and retrieving data from it without disturbing the display and behaviour of the rest of a web page.
Such methods have three parts:
- a PHP script to retrieve the required database info and output it in a way that a Javascript listener can anticipate
- Javascript to invoke the PHP script and deposit the PHP result where it belongs on the page
DIV s in the dropdowns to define where the info is to be written
We'll take them is reverse order.
Declaration of DIVs in dropdowns where AJAX updates are to be written
To make dropdown content visible to a Javascript function, enclose <select>...</select> lists in inline DIV s and give them unique IDs. Here is such a database selection dropdown (a function call has populated the array $adbs with database names):
echo "
<FORM name='DbForm' id='prompt' action='$thispage' method='GET'>\n
<b>Schema:</b> <div id='dbdiv' style='display:inline'>
<select name='db' id='dbsel' onChange='dbonly_submit(this.form)'>";
foreach( $adbs as $d ) {
$sel = ( $db === $d ) ? "id='dsel' selected" : "";
printf( " <option %s value='%s'>%s</option>\n", $sel, $d, $d );
}
echo " </select>\n </div>\n";
The DIV exactly encapsulates dropdown content. As you'll see in the next section, the Javascript function updateAll() uses this fact to rewrite the innerHTML of those DIV s.
Javascript to invoke the PHP script and rewrite the dropdowns
In TheUsual, there are three dropdowns to update—one for databases, one for tables belonging to the selected database, and one for saved queries that have been defined for the selected database. Could we invoke an AJAX method three times in succession?
No, there we run into an AJAX limitation—with multiple consecutive asynchronous calls, all except one are trampled. Instead, the PHP script needs to concatenate the three <select>...</select> blocks; the Javascript function will have to parse the blocks:
<script language="Javascript">
function refresh( s ) {
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null) {
alert( "Browser does not support AJAX." )
return false;
}
xmlHttp.onreadystatechange=updateAll; // NAME OF FUNC TO UPDATE DROPDOWNS
xmlHttp.open("GET","refresh.php?_sess=" + s,true); // ASYNC CALL TO PHP SCRIPT
xmlHttp.send(null);
return true;
}
function updateAll() {
if( (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
&& xmlHttp.status==200
) {
var s = xmlHttp.responseText;
// FIND START OF TABLE DROPDOWN DATA
var i = s.indexOf( " <select name='table'" );
if( i == -1 ) {
return false;
}
else {
document.getElementById("dbdiv").innerHTML = s.substring( 0, i-1 );
s = s.substring( i );
// FIND START OF SAVED QUERIES DATA
var j = s.indexOf( " <select name='view'" );
if( j == -1 ) {
document.getElementById("tbldiv").innerHTML = s;
}
else {
document.getElementById("tbldiv").innerHTML = s.substring(0,j-1 );
if( document.getElementById("viewdiv") in window ) {
document.getElementById("viewdiv").innerHTML = s.substring( j );
}
}
return true;
}
}
}
function GetXmlHttpObject() {
var objXMLHttp=null;
if( window.XMLHttpRequest ) {
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject) {
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
</script>
|