Saturday, March 14, 2009

Basic AJAX code

If you want to have an AJAX application, this simple code is what you must have first.

var xml = make_xml();
function make_xml () {
if (typeof XMLHttpRequest == 'undefined') {
objects = Array(
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.5.0'
);
for (i in objects) {
try {
return new ActiveXObject(objects[i]);
} catch (e) {}
}
} else {
return new XMLHttpRequest();
}
}
Save the code in "ajax.js" and call it in your HTML <head> section like this:
<script language="javascript" type="text/javascript"
src="ajax.js"></script>
Next, you need to have a handle to handle submitted forms like this:
function handle_submit () {
xml.open('post', root + '/');
xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xml.onreadystatechange = function () {
if (xml.readyState == 4) {
data = xml.responseText.split ( "[BRK]" );
get('div1').innerHTML = data[0];
get('div2').innerHTML = data[1];
} else {
get('div1').innerHTML = 'Please wait';
get('div2').innerHTML = 'Please wait';
}
}
if (window.encodeURI(get('action').value) == 'action1') {
xml.send('action=' + window.encodeURI(get('action').value) + '&javascript=1&
field1=' + window.encodeURI(get('field1').value) + '&field2=' +
window.encodeURI(get('field2').value));
} else if (window.encodeURI(get('action').value) == 'action2') {
xml.send('action=' + window.encodeURI(get('action').value) + '&javascript=1');
}
return false;
}
In order to save time, I prefer to replace the long "document.getElementById" with a simple get() function, like this:
function get (id) {
return document.getElementById(id);
}
Next, you need to have a handle to load onsubmit action, like this:
function load_handler () {
get('action1').onsubmit = handle_submit;
}
Don't forget to call the handle during page load, like this:
window.onload = load_handler;

Using this basic AJAX code, you may do all the web interactivity using AJAX, easily.

All the other codes didn't change at all, including HTML forms, PHP codes, etc. All you have to do is just synchronize the ID's of your HTML forms with your "ajax.js" ID's, otherwise it won't recognize your request.

Any questions, please write on the comment form. Good luck!

Thursday, January 22, 2009

Happy New Year!


I'm writing this post to wish my Chinese friends a Gong Xi Fa Chai!

I also wanted to say sorry for not writing new codes to you. This is because I'm currently too busy doing my "Mega Project" for my big client.

I'm hoping to write more tips and tricks in programming more consistently.

Have a good day! :)

Saturday, November 29, 2008

I am still alive

It has been a while I didn't update this blog, since I was too busy managing a sports event at my place.

Today I'm not going to post anything about PHP functions. Instead, I would like to give a big thank my loyal readers on being such a concern asking me if I'm still alive.

I'm telling you guys, I'm still alive healthily and happily right now. I won 4 medals in the annual event — gold medals for badminton and rope pulling, silver medals for volleyball and soccer. What a worth busy moments!

After all, thank you for being so patient waiting for my updates. I'll definitely update this blog soon.

Saturday, October 18, 2008

Fortunate vs Unfortunate

My hard disk crashed, totally damaged. This is real sick.

Fortunately it is still under warranty. Unfortunately it takes too much time to be replaced.

Fortunately not more than a month to wait. Unfortunately I lost all my data.

Fortunately I got backup. Unfortunately the last one was about three months ago.

Fortunately I can get back all my PHP applications. Unfortunately not the latest one.

Fortunately I can get back my MySQL data, too. Unfortunately not the latest one, too.

Fortunately I can set back my work after the new one arrived. Unfortunately I didn't backup all my software as they eat lots of disk space.

OMG, I don't know what to do, where to start, how to complete. It's real hard.

What do you think when fortunate vs unfortunate. Which one wins?

Saturday, September 27, 2008

Prevent accidental window closing: alert onbeforeUnLoad

Today, I'm going to show you how to stop your visitor to close window of your site, using JavaScript.

It's useful when they have unfinished processes, or unsaved data, in which they whether accidentally or deliberately clicked on the close button of your page.

It's also useful for your marketing strategy, like one-time offer, final discounts, and so on.

Simple. First, use this following function. Then I'll show you with 3 situations for example.

<script>
var askFirst = false;
function preventerOn() {
askFirst = true;
}

function preventerOff() {
askFirst = false;
}
window.onbeforeunload = sureUnload;
function sureUnload() {
if (askFirst)
return "Are you sure you want to exit this page?";
}
</script>


Situation 1, if you want to prevent window closing from the beginning, you may want to use the body tag, like this:

<body onload="return preventerOn();">


Situation 2, if you want to prevent window closing when your visitor enter something in a form, use it like this:

<input type="text" name="input1" onchange="return preventerOn();">


Situation 3, if you want to prevent window closing before reached an amount of time (i.e: 20 seconds), you may add this one to the script above:

function setTimer() {
preventerOn();
setTimeout("clear();",20000); // 1000 is for 1 second, so 20000 is for 20 seconds
}
function clear() {
preventerOff();
}


You may need to change in the body tag, from this:

<body onload="return preventerOn();">


to this:

<body onload="return setTimer();">


That's all.

If you have any questions regarding this, please feel free and don't hesitate to ask me through the comment form.

Thank you.