[Cialug] java web toolkits
carl-olsen at mchsi.com
carl-olsen at mchsi.com
Wed Dec 12 10:54:20 CST 2007
Here's my JavaScript:
var xmlHttp = false;
var serverAddressStates = "http://localhost/applications/admission/states.php?action=GetStates";
var serverAddressCountries = "http://localhost/applications/admission/locations.php?action=GetCountries";
var updateInterval = 5;
var errorRetryInterval = 30;
var debugMode = true;
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
{
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
}
if(!xmlHttp)
{
alert("Error creating the XMLHttpRequest object.");
}
function display(msg)
{
var divState = document.getElementById("select_state");
divState.innerHTML = "<select id=\"input_selectedindex\" name=\"input_selectedindex\" style=\"width: 206px; background-color: #ffffff; color: #000000;\">" + msg + "</select>";
}
function displayError(msg)
{
display("Error retrieving the news message! Will retry in " +
errorRetryInterval + " seconds." +
(debugMode ? "<br/>" + msg : ""));
setTimeout("processStates();", errorRetryInterval * 1000);
}
function processStates()
{
if (xmlHttp)
{
try
{
//display("Receiving new message from server...")
xmlHttp.open("GET", serverAddressStates, true);
xmlHttp.onreadystatechange = handleGettingRequest;
xmlHttp.send(null);
}
catch(e)
{
displayError(e.toString());
}
}
}
function handleGettingRequest()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
try
{
getRequest();
}
catch(e)
{
displayError(e.toString());
}
}
else
{
displayError(xmlHttp.statusText);
}
}
}
function getRequest()
{
var response = xmlHttp.responseText;
if (response.indexOf("ERRNO") >= 0
|| response.indexOf("error") >= 0
|| response.length == 0)
{
throw(response.length == 0 ? "Server error." : response);
}
display(response);
// window.setTimeout("processStates();", updateInterval * 1000);
}
Here's one of the PHP pages it calls:
<?php
require_once("admission/javascript/error_handler.php");
function __autoload($class)
{
require_once("admission/class/class." . $class . ".php");
}
$db = new Data();
header("Expires: Wed, 23 Dec 1980 00:30:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
$action = $_GET["action"];
$item = $_GET["item"];
if ($action == "GetStates")
{
$location = new Location($db);
$states = $location->getStates($item);
print($states);
}
elseif ($action == "GetCountries")
{
$country = new Country($db);
$countries = $country->getCountries($item);
print($countries);
}
elseif ($action == "GetLists")
{
$list = new Table($db,"l");
$lists = $list->getLists($item);
print($lists);
}
else
{
print("Communication error: server doesn't understand command.");
}
?>
There's a database abstraction layer missing here, but this should give you a general idea of how to role your own ajax.
I think I got this code from the book "AJAX and PHP" by PACKT Publishing.
Carl
-------------- Original message ----------------------
From: "Dave J. Hala Jr." <dave at 58ghz.net>
>
>
> I could really make use of dynamic option list too, especially, if it
> could read its options from a mysql table.
>
> On Wed, 2007-12-12 at 10:44 -0600, David Champion wrote:
> > Matthew Nuzum wrote:
> > > On Dec 12, 2007 9:37 AM, <carl-olsen at mchsi.com> wrote:
> > >
> > >
> > >> I would not use a framework for this unless I had other reasons for using
> > >> a framework. I'm playing with symfony right now, but it will not run
> inside
> > >> the CMS I have to integrate it with. I'm using the ORM part of symfony to
> > >> create my PHP objects, which is definitely a big advantage.
> > >>
> > >> Carl
> > >>
> > >>
> > >
> > > Well, there are js only frameworks too. For Ubuntu.com I use mootools, but
> > > jquery is similar (and more data focused, where moo is more visual/effect
> > > focused). These libraries are relatively lightweight if you don't include
> > > all the bells and whistles. They make loading json (or whatever) extremely
> > > simple. Other benefits include simplifying your usual JS code. In moo I do
> > > something like this to track external links:
> > >
> > > $$("a[href^='http://']").each(function(link) {
> > > link.addEvent('click', [code to do onclick])
> > > }
> > >
> > > That gets all the "A" tags that have an href that matches the regex ^http://
> > > and *adds* (not replaces) an onclick function to them.
> > >
> > > Ajax is as simple as:
> > >
> > > window.addEvent('domready', function() {
> > > xhr.request().chain(selectmirror);
> > > })
> > >
> > > var selectmirror = function () {
> > > [deal with the json/xml]
> > > }
> > >
> > > However, learning the framework is a chore that you have to accomplish in
> > > order to get the maximum benefit. So the upfront cost is high but the payoff
> > > is long term.
> > >
> > Is there some simple AJAX lib for Java that would be an equivalent of
> > xajax for PHP? That's so stupid simple that even I can do it. It's not
> > perfect, but it make adding AJAX to an existing PHP app very easy. The
> > first thing I used it for was exactly what DW is wanting to do - a
> > dynamic option list.
> >
> > -dc
> >
> >
> > _______________________________________________
> > Cialug mailing list
> > Cialug at cialug.org
> > http://cialug.org/mailman/listinfo/cialug
>
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
More information about the Cialug
mailing list