473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = createAjaxObjec t();
ajax.onreadysta techange = handleAjaxRetur n;
ajax.open("GET" , url, true);
return ajaxReturned;
}

"createAjaxObje ct" is a function that I use all the time that
initializes the request.

So, I have this function to handle the readystatechang es:

function handleAjaxRetur n(){
if (ajax.readystat e == 4){
if (ajax.status == 200){
ajaxReturned = ajax.responseTe xt;
}
}
}

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="dateval idate(this)" />

and a script like this:

function datevalidation( field){
if (field.value != ""){
var eVal = escape(field.va lue);
var vDate = ajaxresult("dat echeck.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 handleAjaxRetur n()? 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 handleAjaxRetur n() 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 3494
Hi,

ajax.open(_sth_ , _sth_, true)

means that call is asynchronous, so "handleAjaxRetu rn()" 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.n etwrote:
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= createAjaxObjec t();
ajax.onreadysta techange = handleAjaxRetur n;
ajax.open("GET" , url, true);
return ajaxReturned;
}

"createAjaxObje ct" is a function that I use all the time that
initializes the request.

So, I have this function to handle the readystatechang es:

function handleAjaxRetur n(){
if (ajax.readystat e == 4){
if (ajax.status == 200){
ajaxReturned =ajax.responseT ext;
}
}
}

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="dateval idate(this)" />

and a script like this:

function datevalidation( field){
if (field.value != ""){
var eVal = escape(field.va lue);
var vDate = ajaxresult("dat echeck.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 handleAjaxRetur n()? 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 handleAjaxRetur n() 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************ *********@p15g2 000hsd.googlegr oups.com>,
"Pazabo" <pa****@gmail.c omwrote:
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= createAjaxObjec t();
ajax.onreadysta techange = handleAjaxRetur n;
ajax.open("GET" , url, true);
return ajaxReturned;
}

"createAjaxObje ct" is a function that I use all the time that
initializes the request.

So, I have this function to handle the readystatechang es:

function handleAjaxRetur n(){
if (ajax.readystat e == 4){
if (ajax.status == 200){
ajaxReturned =ajax.responseT ext;
}
}
}

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="dateval idate(this)" />

and a script like this:

function datevalidation( field){
if (field.value != ""){
var eVal = escape(field.va lue);
var vDate = ajaxresult("dat echeck.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 handleAjaxRetur n()? 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 handleAjaxRetur n() 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 "handleAjaxRetu rn()" 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.n etwrote:
ajax.open(_sth_ , _sth_, true)
means that call is asynchronous, so "handleAjaxRetu rn()" 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
4862
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 object. If the string I am passing to javascript from php is too large javascript does not get it all the data. The magic number appears to be 6123 characters, anything below that it works fine, anything above and if I alert the ajax.response, I see...
7
3620
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
5168
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 application working. However, when attempting to implement the solution, the AJAX calls weren't updating the screen like the examples were and seemed not to fire until after the long running process had completed. I found the only real...
8
2124
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
3173
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 i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand whats the prob.this is the code i m using: <? include("config.inc.php"); //error_reporting(0); ...
1
3415
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 that I see in the comments, but no luck there), but what I already have works GREAT to populate the options in a select box. Now I'm trying to take that same code and get it to write the value to a couple input boxes. Here's a link to my ajax...
1
2020
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 the contents from a server side script as AJAX generally does and process it using that function. This far Im quite pleased with it, however, I really want something that will allow me to perform multiple AJAX calls from the same script. I...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.