Simple XMLHttpRequest Code

I have been playing with XMLHttpRequest a bit more and have created a simple application that is available here. Using a free database of zip codes and their corresponding cities and latitudes and longitudes I created a simple application that calculates the distance between two zip codes.

Notice, the page does not reload when you click the “Calculate” button, it simply fills in the blank even though it is making a database call.

index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head>

<mce:script language="javascript" type="text/javascript">

<!--
var url = "getDistance.php?";
function handleHTTPResponse(){
if(http.readyState == 4)
{
var xmlDocument = http.responseXML;
var distance = xmlDocument.getElementsByTagName('miles').item(0).firstChild.data;
document.getElementById('distance').value = distance;
//document.getElementById('url').value = url;
}
}

function updateDistance(){
url = "getDistance.php?";
var zip1 = document.getElementById("zip1").value;
var zip2 = document.getElementById("zip2").value;
url = url + "zip1=" + escape(zip1) + "&#038;zip2=" + escape(zip2);
http.open("GET", url, true);
http.onreadystatechange = handleHTTPResponse;
http.send(null);
}

function getHTTPObject()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp &#038;& typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType("text/xml");
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
// --></mce:script>
</head>
<body>

<form action="post">
<p>First Zip Code:</p> <input type="text" size="5" name="zip1" id="zip1" /> <p> Second Zip Code:</p> <input type="text" size="5" name="zip2" id="zip2"/> <p> Distance:</p> <input type="text" size="10" name="distance" id="distance"/> <input type="button" value="Get Distance" id="submit" onclick="updateDistance();"/> </form> </body></html>

In this code we are simply creating the Javascript objects that we’ll need to do the calculation. The updateDistance() function does a lot of the easy work, simply setting the values pulled from the form. handleHTTPResponse() does most of the heavy lifting, getting the values from the called PHP that returns a basic XML file.

PHP:

This PHP file does the actual work. It pulls the needed values from the database, then calculates the distance using the Great Circle Distnace formula. It then returns an XML file that is called by the above Javascript.

Generated XML file:

It’s a very simple XML file that returns the basic data needed. The most important field is the ‘miles’. It is a rounded distance returned by the distance formula.

So that’s that in a nutshell. It is a lot simplier than it looks and it gets the job done. Of course there are downsides to using XMLHttpRequest and you can do a search on Google for such reasons (maybe that will be my next post, “The downfalls of XMLHttpRequest”).

If you have any questions about how all of this works feel free to e-mail me at s [at] badice [dot] com.

Don’t Call it That!

As I mentioned in my last post I mentioned starting to play with Ajax. Well, I have been informed that “Ajax” is a “stupid name”.

Ajax is a name for the grouping of XMLHttpRequest, DOM, and CSS together to create web applications that run seamlessly with the backend to add dynamic information to the page.

Well, the Ajax name was first used by the folks at Adaptive Path, who some believe coined the term just to get their name out there.

Do I really care? Not really, I know that I want to use XMLHttpRequest, DOM, and CSS to do some new and cool things and that’s that. I don’t really care about the name. I could just give it a name that suits me so that I don’t have to read out “We’ll be using XMLHttpRequest, DOM, and CSS” because that’s a mouthful (hmmm, maybe that’s why Adaptive Path came up with the name).

Playing With Ajax

I am going to be doing some playing with Ajax over the next few days and hopefully I will get some code examples up when I have the chance.

My current dilemma is figuring out whether or not Ajax is what I need and where it could be useful and where it would be cumbersome. I know that as a whole it is a very awesome technology, just the fact that a page does not have to be reloaded to perform a server side action is amazing. The real issue is how usable is Ajax and what applications could I use it in.

The first type of application where it would be extremely useful is something that requires a lot of user input. If you can limit the amount of input needed from the user and limit the number of times they have to use their mouse to click a button the more productive they become. The one application that comes to mind is the user’s interface on Flickr. If you want to edit a picture’s title or description you just have to click on the text, edit it, and click the “save” button. There is no clicking on a “Edit this picture” button or link and there is no need to fill out form fields that don’t need to be edited, it’s all done right on the same screen. Very convenient and to me, intuitive. It makes sense to edit things like that on the page in which the show up. Plus, you are not taxing the server (as much) because you do not have to reload the page, recalling all of the information that you already have up, you just have to reload the information that was changed.

It is just a very interesting concept and suprisingly easy to understand (the concept at least). I guess the next thing on my purchase list is a JavaScript book.

So What’s This “PowerBook Fund” Thing?

If you are a regular reader of my blog then you probably noticed the “PowerBook Fund” piece on the left side. I’ll explain.

I am in the market for a 15″ PowerBook. Being an MIS student leaves me needing something to code on while I’m in class. I like being able to do what the professor is doing on screen and since all of my classrooms do not have computers sometimes I just do not have the opportunity to be the “hands-on learner”.

I have started my own little saving for the PowerBook but thought I would open it up for donations. I plan on buying a base 15″ model with a RAM upgrade. If you’d like to contribute feel free, any amount is greatly appreciated. I am going to be adding some stuff to the sidebar to convey my goal and how close I am to reaching it.

I am not the type for handouts but I really felt like asking loyal readers for contributions would not be a huge deal. The Google AdSense ads do not bring in a ton of money (very little actually) and contributions seem like a viable option.

If you have any questions about all of this you can e-mail me at s [at] badice dot com.

More on Schiavo

I do not think I have ever been more dissappointed with our political system in my entire life until now. Since when does Congress have the right to become a judicial branch of the government? The entire Schiavo case should have never have made it this far but now that it has I have realized that it is a bigger problem than most people see.

The question of quality of life is the issue and the husband, by law has the right to decide if the feeding tube should or should not be removed. End of discussion. That’s the law. I don’t care if he is seeing another woman, he is still married to Terri Schiavo and from talking to people and doing a little research there is little he can do to get a divorce. There is no one who can honestly tell me that Terri Schiavo is “living”, she has been in a vegetative state for 15 years, 15! She is currently in a hospice, which last time I checked was for palliative treatment, not curative. A hospice, by definition neither postpones nor hastens death. To me this would mean that the feeding tube be removed, because technically it is postponing death. A hospice is supposed to provide care to patients who have a limited life expectancy, Terri Schiavo has been alive artificially for 15 yrs.

Don’t get me wrong, I am all for treating the sick, but this is not treatment, it is postponing what people know will happen, they just don’t know when.

I know that fellow Republicans are reading this and saying to themselves that I am not a true member of the party, well, if that’s the way you feel it’s too bad. I am a pro-life person but this is not life, it is suffering at it’s worst and we (the government) have let it go too far. Congress has no right to act the way it has and I am very interested to see what kind of holes this opens up for other people. We are watching the demise of our political system unfold right before our eyes.

Texas Tech and the Sweet 16

Texas Tech has advanced to the Sweet 16 after defeating Gonzaga.

From the beginning Tech was not performing well and wasn’t keeping up with the Gonzaga offense. Going into halftime Gonzaga had a 13pt. but Tech came back and outperformed Gonzaga, with some great 3pt. shots and a lot of free throws.

It was a scary performance for Tech and in the next round they will definitely need to play like they played against UCLA in the first round.

Being Controversial?

I am going to go out on a limb here and maybe be a little controversial. Right now there is a big debate over the Terri Schiavo case and who has the right to say whether her feeding tube is removed or not. To me this is a non-issue. She is still married and by law her husband has the legal right to remove the tube. I know that people want to use the fact that he is seeing another woman and such to say that he should not be married to her and therefore have no right to remove the tube. But who are we to determine that they should not be married. At what point do we retract ourselves from other’s lives and let them choose their path.

On one hand I can see where Terri Schiavo’s parents are coming from. They love their daughter and do not want to see her pass away. However, to me, Mrs. Schiavo is already dead, mentally at least, and maybe legally. When your heart stops you are not declared dead, actually your heart could still be beating and you are declared dead. The brain is a hospital’s main way of determining the time of death and when a person has actually been lost. Terri Schiavo is showing just enough brain activity to not be declared dead. The doctors are saying the chances of her ever coming out of the vegetative state are very slim to none.

The basic question then comes down to right to life. I agree with the principle that people (babies, children, adults, etc.) have a right to life. I don’t agree with abortion but this case really has me torn. Terri Schiavo, to me at least, is already deceased. Her brain is unable to function enough to support itself and from the medical side of things that is teetering on death.

I know that this is an extremely controversial issue but it really struck me today that people are throwing themselves into the middle of this like they have something at stake. If I ever was in a similiar vegetative state I know that I would not want to be like that forever, until the money ran out. I know I cannot speak for Terri but I can speak for friends who wear “Do not resuscitate” bands on them so that if they are horribly injured they do not have to endure what Terri Schiavo is enduring right now (even if she cannot cognitively grasp it).