473,323 Members | 1,551 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,323 software developers and data experts.

variables and ajax

I'm working on a "simple" tracking system for a client that will be
receiving elearning packages from 3rd parties.

I'm intending to do the communication with the database using ajax.

My problem is one of concepts. An elearning package, of which I have no
control over, might have code that looks like this;

1: var val = GetValue("name");
2:
3: if (val) {
4: doThis();
5: } else {
6: doThat();
7: }

Line 1 would trigger an ajax call to the db but as the call is
asynchronous does that mean that at line 3 "val" may be undefined?

I'm still struggling with ajax concepts. I can understand how to, say,
update a database but simple things like getting a value into a variable
is escaping me.

Andrew Poulos
Nov 9 '08 #1
7 1441
Andrew Poulos meinte:
I'm working on a "simple" tracking system for a client that will be
receiving elearning packages from 3rd parties.

I'm intending to do the communication with the database using ajax.

My problem is one of concepts. An elearning package, of which I have no
control over, might have code that looks like this;

1: var val = GetValue("name");
2:
3: if (val) {
4: doThis();
5: } else {
6: doThat();
7: }

Line 1 would trigger an ajax call to the db but as the call is
asynchronous does that mean that at line 3 "val" may be undefined?
Yes. GetValue() (BTW: better name it "getValue" - it's no constructor)
sends the request. End of story for this part of the script. Once the
response is being returned a callback function assigned to the
onreadystatechange property of the XHR-Object is triggered. This
callback function can now decide whether val is set and in turn invoke
doThis() or doThat(). It's explained in detail an (in)numerous websites
- here's one:
https://developer.mozilla.org/En/Using_XMLHttpRequest
I'm still struggling with ajax concepts. I can understand how to, say,
update a database but simple things like getting a value into a variable
is escaping me.
In the first case you do not necessarily need a callback function. In
the latter case you do.

Gregor
Nov 9 '08 #2
Gregor Kofler wrote:
Andrew Poulos meinte:
>I'm working on a "simple" tracking system for a client that will be
receiving elearning packages from 3rd parties.

I'm intending to do the communication with the database using ajax.

My problem is one of concepts. An elearning package, of which I have
no control over, might have code that looks like this;

1: var val = GetValue("name");
2:
3: if (val) {
4: doThis();
5: } else {
6: doThat();
7: }

Line 1 would trigger an ajax call to the db but as the call is
asynchronous does that mean that at line 3 "val" may be undefined?

Yes. GetValue() (BTW: better name it "getValue" - it's no constructor)
sends the request. End of story for this part of the script. Once the
response is being returned a callback function assigned to the
onreadystatechange property of the XHR-Object is triggered. This
callback function can now decide whether val is set and in turn invoke
doThis() or doThat(). It's explained in detail an (in)numerous websites
- here's one:
https://developer.mozilla.org/En/Using_XMLHttpRequest
Thanks, I follow what you're saying but I get lost with how the callback
function knows which variable to assign a value to. Would I need to hard
code the variable name into the callback?

Andrew Poulos
Nov 9 '08 #3
Andrew Poulos meinte:
Thanks, I follow what you're saying but I get lost with how the callback
function knows which variable to assign a value to. Would I need to hard
code the variable name into the callback?
What do you mean by "hard code". A closure is all you need.

var foo;
....

xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}

....

Gregor
Nov 10 '08 #4
Gregor Kofler wrote:
Andrew Poulos meinte:
>Thanks, I follow what you're saying but I get lost with how the
callback function knows which variable to assign a value to. Would I
need to hard code the variable name into the callback?

What do you mean by "hard code". A closure is all you need.

var foo;
...

xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}
I don't know what the names of the variable(s) that a 3rd party
elearning package may use. So I can't write
foo = xhr.responseText;
as the variable name could be anything.

Andrew Poulos
Nov 10 '08 #5
Andrew Poulos wrote:
Gregor Kofler wrote:
>Andrew Poulos meinte:
>>Thanks, I follow what you're saying but I get lost with how the
callback function knows which variable to assign a value to. Would I
need to hard code the variable name into the callback?

What do you mean by "hard code". A closure is all you need.

var foo;
...

xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}

I don't know what the names of the variable(s) that a 3rd party
elearning package may use. So I can't write
foo = xhr.responseText;
as the variable name could be anything.
Would a synchronous call be appropriate in this situation? That is:

var foo = getValue("name");

function getValue(n) {
// ajax stuff here
...
xhr.open("post","test.php", false);
xhr.send(passData);
return xhr.responseText:
...
}

Andrew Poulos
Nov 10 '08 #6
On Nov 10, 8:14 am, Andrew Poulos <ap_p...@hotmail.comwrote:
Gregor Kofler wrote:
Andrew Poulos meinte:
Thanks, I follow what you're saying but I get lost with how the
callback function knows which variable to assign a value to. Would I
need to hard code the variable name into the callback?
What do you mean by "hard code". A closure is all you need.
var foo;
...
xhr.onreadystatechange = function() {
...
foo = xhr.responseText;
}

I don't know what the names of the variable(s) that a 3rd party
elearning package may use. So I can't write
foo = xhr.responseText;
as the variable name could be anything.
Specify the API as expecting a callback:

// API will pass value as first parameter of callback:
getValue(identifier,callback);

// call it like:

getValue("name", function (foo) {
// 3rd party developer does what he wants to do with foo in here
});

// alternatively, can also be called like:

function myCallback (x) {
// do stuff with x here
}
getValue("name",myCallback);

/*
A possible implementation of getValue is something like:
*/

function getValue(n,fn) {
xhr = new XMLHttpRequest();
...

xhr.onreadystatechange = function () {
...

fn(xhr.responseText);
}
}
Nov 10 '08 #7
Andrew Poulos wrote:
Would a synchronous call be appropriate in this situation? That is:

var foo = getValue("name");

function getValue(n) {
// ajax stuff here
...
xhr.open("post","test.php", false);
xhr.send(passData);
return xhr.responseText:
...
}
Synchronous request-response handling would be the only viable option with
this approach. Whether that approach would be appropriate here is quite a
different matter, though; it would depend on the kind of request, the
responsiveness that can be expected of the server, and the responsiveness
demanded from the application. Synchronous request-response handling has a
strong tendency to suspend all other threads of the user agent while it is
in progress.
HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 10 '08 #8

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

Similar topics

2
by: napawlm | last post by:
Is there a way to "pass" an XMLHttpRequest object to the callback function for onreadystatechange? Or a way to access it from onreadystatechange? I would like to avoid the use of a global...
7
by: Geoff Cox | last post by:
Hello, I have been able to create a slider using Javascript code from the web but would like to send the slider values which are saved in an array to myself using email and the CGI/form...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
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...
3
by: radix | last post by:
Hello, I have a aspx page with ajax scriptmanger update panel etc. I also have public string variables on the aspx page. Whenever postback happens from Ajax update panel, at server side all...
11
by: julie.siebel | last post by:
I'm working on a rather complex booking system for building European trips, in a combination of SQL/VBScript/Javascript. There are tons of query string variables that get passed back and forth...
3
gchq
by: gchq | last post by:
Hi there Passing variables to a new window was quite easy before Ajax, just write a window.open script and include it under the button click event with the variables being including in the url and...
5
by: quirk | last post by:
I am trying to write a script where a page is populated with some maths questions, user answers them (it's timed but I've left this bit out), gets results on same page and ajax takes their score,...
11
by: zanzo | last post by:
usually we create a url with parameters and values and send it via ajax to not loading page. Is there a way to not send variables in the url BUT using hidden variables?? I have a problem in my...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.