One thing that may be useful in our Web 2.0 era is the support of AJAX functionality, i.e. the processing of server-side requests without reloading the whole page.
The good news is that it is supported in CSE_Display. The bad news is that it is not completely trivial to use it.
But first, a small demo.
Type here and click | Server answers here |
|
The code for this is:
<?php
$myHTML->activateAJAX(); // the library is in its default location, otherwise give location as parameter!
$myHTML->appendBody("<input type=button value=\"Request\" onClick=\"".($myHTML->ajaxIt("", "echo", "text1<:>value", "text2<:>value", true))."\">"); // this last true is for showing the traffic in JS alerts. Good for debugging, remove it for normal use.
?>
It is pretty easy, because I actually use one of the few default functions 'ajax_echo'. But it gets a bit more complex when you want to define your own functions - which is very likely to be the case.
Let's build, for instance, a 'suggestion' box, suggesting names as you type them in a box. Without special effects of popup menus.
We first need a function to handle the request. Let's make a ajax_reply.php containing the function ajax_suggest():
<?php
function ajax_suggest($params, $target) {
$search_key = $params[0]; // always an array.
// this would probably a database call in a normal application, using SQL search function.
// but for here without database backend:
$mtch = "";
$mylist = array("Albert", "Abdul", "Bastien", "Charlene", "Christian", "Laurent", "Laurence", "Lorenzo", "Leonie");
$ignore = sort($mylist);
for ($i=0; $i<count($mylist);$i++) {
if (stripos("£".$mylist[$i],"£".$search_key)!==false) {
$mtch .= ",".$mylist[$i];
}
}
if (strlen($mtch)==0) {
$mtch = "No Match";
} else {
$mtch = substr($mtch,1,strlen($mtch)-1);
if (strlen($mtch)>25) {
$mtch = substr($mtch,0,22)."...";
}
}
// Now we have what we want in the $mtch variable, let's format the result:
return $target."<:>".$mtch."<:>";
// We could modify other properties of the page by appending further strings like
// return $target."<:>".$mtch."<:><|>text2<:>value<:>String to put in<:>";
}
?>
Now we add in the HTML file the required elements for the script, ajax-ing the onKeyUp
<?php
$myHTML->appendBody("<table><tr><td>Name:</td><td><textarea id=text3 cols=30 rows=2 onKeyUp=\"".($myHTML->ajaxIt("ajax_reply.php", "ajax_suggest", "text3<:>value", "text4<:>innerHTML"))."\"></textarea><br>");
$myHTML->appendBody("</td><td>Propositions: <div id=text4> </div></td></tr></table>");
?>
Name: | Propositions: |
And you can test the applet!
Note: this system allow the client to launch a php function on the server with parameters of his choice. Now this is potentially a security issue. For this reason, all callback files and functions MUST have names that begin with "ajax_", otherwise the server will refuse to execute the call.