S
Sandman
So, I've used ajax for quite some time for different stuff. Mostly I
just feed a funktion I made with the ID of the DIV that should be
updated with the output from page XXX.php
Now I want to have a standard funktion to set a JS variabel to the
output of a page. I am doing a date validation thing, which is done in
PHP. So this is how far I've come so far:
function ajaxresults(url){
var ajax = createAjaxObject();
ajax.onreadystatechange = handleAjaxReturn;
ajax.open("GET", url, true);
return ajaxReturned;
}
"createAjaxObject" is a function that I use all the time that
initializes the request.
So, I have this function to handle the readystatechanges:
function handleAjaxReturn(){
if (ajax.readystate == 4){
if (ajax.status == 200){
ajaxReturned = ajax.responseText;
}
}
}
This function sets "returned" to the value of the finnished script.
So, now I want to use these function in a practical example:
On the page, I might have this:
<input type='text' name='date' onblur="datevalidate(this)" />
and a script like this:
function datevalidation(field){
if (field.value != ""){
var eVal = escape(field.value);
var vDate = ajaxresult("datecheck.php?d=" +eVal;
field.value = vDate
}
}
So, basically, the idea is that the value of the input field should be
replaced by the output of datecheck.php. But it isn't, since vDate is
"undefined"
It is undefined because ajaxresult() doesn't return any value. And
ajaxresult() doesn't return any value because it ends before the HTTP
request reaches readystate 4.
So, basically, how do I construct the two first functions so that
ajaxresult() returns the data from the ajax object that is processed
by handleAjaxReturn()? The last function must remain intact, since I
want to be able to use this as a standard function for various
functions. I.e. nothing in ajaxresults() or handleAjaxReturn() must
relate to date validation or setting the value of the field in my
example. ALl they should do is fetch the output of a given URL and
return it to the caller.
So, any ideas?
just feed a funktion I made with the ID of the DIV that should be
updated with the output from page XXX.php
Now I want to have a standard funktion to set a JS variabel to the
output of a page. I am doing a date validation thing, which is done in
PHP. So this is how far I've come so far:
function ajaxresults(url){
var ajax = createAjaxObject();
ajax.onreadystatechange = handleAjaxReturn;
ajax.open("GET", url, true);
return ajaxReturned;
}
"createAjaxObject" is a function that I use all the time that
initializes the request.
So, I have this function to handle the readystatechanges:
function handleAjaxReturn(){
if (ajax.readystate == 4){
if (ajax.status == 200){
ajaxReturned = ajax.responseText;
}
}
}
This function sets "returned" to the value of the finnished script.
So, now I want to use these function in a practical example:
On the page, I might have this:
<input type='text' name='date' onblur="datevalidate(this)" />
and a script like this:
function datevalidation(field){
if (field.value != ""){
var eVal = escape(field.value);
var vDate = ajaxresult("datecheck.php?d=" +eVal;
field.value = vDate
}
}
So, basically, the idea is that the value of the input field should be
replaced by the output of datecheck.php. But it isn't, since vDate is
"undefined"
It is undefined because ajaxresult() doesn't return any value. And
ajaxresult() doesn't return any value because it ends before the HTTP
request reaches readystate 4.
So, basically, how do I construct the two first functions so that
ajaxresult() returns the data from the ajax object that is processed
by handleAjaxReturn()? The last function must remain intact, since I
want to be able to use this as a standard function for various
functions. I.e. nothing in ajaxresults() or handleAjaxReturn() must
relate to date validation or setting the value of the field in my
example. ALl they should do is fetch the output of a given URL and
return it to the caller.
So, any ideas?