473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Building a standard AJAX function

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?

--
Sandman[.net]
Feb 27 '07 #1
3 3465
Hi,

ajax.open(_sth_, _sth_, true)

means that call is asynchronous, so "handleAjaxReturn()" is run after
response comes from the server, while "return ajaxReturned" acts
immediately (you don't know the returned value yet).

On Feb 27, 12:00 pm, Sandman <m...@sandman.netwrote:
So, I've usedajaxfor 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){
varajax= 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 theajaxobject 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?

--
Sandman[.net]

Mar 25 '07 #2
In article <11*********************@p15g2000hsd.googlegroups. com>,
"Pazabo" <pa****@gmail.comwrote:
So, I've usedajaxfor 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){
varajax= 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 theajaxobject 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?

Hi,

ajax.open(_sth_, _sth_, true)

means that call is asynchronous, so "handleAjaxReturn()" is run after
response comes from the server, while "return ajaxReturned" acts
immediately (you don't know the returned value yet).
Yes, that's exactly what I wrote in my post. So what do I do about it?
--
Sandman[.net]
Mar 25 '07 #3
VK
On Mar 25, 10:53 pm, Sandman <m...@sandman.netwrote:
ajax.open(_sth_, _sth_, true)
means that call is asynchronous, so "handleAjaxReturn()" is run after
response comes from the server, while "return ajaxReturned" acts
immediately (you don't know the returned value yet).

Yes, that's exactly what I wrote in my post. So what do I do about it?
"AJAX Solutions" over the last two years effectively replaced the age
old drop down menu hobby. Before that anyone anyhow involved in
JavaScript/DOM/CSS felt obligated to write at least one dynamic menu
by her own. I say if we put all menus of all kinds ever written across
the globe since the 4th versions release, in open state they would
make a chain from the Earth to the Moon :-) Respectively I'm scared to
imagine the total bandwidth of ever made "AJAX" even if we take only
more-or-less working ones. :-)

This long but - I hope - not nasty preface is to explain why for you
question
- what do I do about it?
the answer is:
- get yourself a cross-tested long existing cross-browser library with
a robust multiple requests support. In this NG it is traditionally
suggested - and no complains so far - AjaxRequest by Matt Kruse
http://www.ajaxtoolbox.com

Mar 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: trpost | last post by:
I am using ajax / php where I am looking up some info from the database and populating a select list dynamically, however I am running into some sort of size limitation with the ajax.response...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
8
by: jd2007 | last post by:
Why the Ajax code below in ajax.js is causing my form not to work ? ajax.js: var a=0; var b=0; var c=0; var d=0; var e=0; var f=0;
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
1
by: shaunwo | last post by:
I'm an AJAX / DOM Novice (at best) and trying to figure out how to write the value to a couple input fields. I don't remember exactly where I got the ajax.js file I'm using from (went to the website...
1
by: bizt | last post by:
Hi, I am having my first real attempt at an ajax class as so far Ive managed to build one that, once instatiated, will allow me to define which function it will call on completion then retrieves...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.