/    Sign up×
Community /Pin to ProfileBookmark

Passing Variable

Hi, I wonder if anyone could help:

I have the following functions set up to retrieve data from a php file:

[CODE]
function create_request_object()
{
var request_o;
var browser = navigator.appName;

if(browser == “Microsoft Internet Explorer”)
{
request_o = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else
{
request_o = new XMLHttpRequest();
}
return request_o;
}

function get_value(req_url)
{
http.open(‘get’, req_url);
http.onreadystatechange = handle_data;
http.send(null);
}

function handle_data()
{
if(http.readyState == 4)
{
var response = http.responseText;

document.getElementById(content_container).innerHTML = response;
document.getElementById(content_container).style.display = ‘block’;
// hide any elements if requiured
for (i = 0; i < window.content_hide.length; i++)
{
document.getElementById(window.content_hide[i]).style.display = ‘none’;
}
// show any elements if requiured
for (i = 0; i < window.content_show.length; i++)
{
document.getElementById(window.content_show[i]).style.display = ‘block’;
}
}
}
[/CODE]

I then call the functions later in the script

[CODE]
get_value(req_url);
[/CODE]

This is probably really simple, but I just can’t get it to work. What I want, is for some way of knowing that response was populated and the html element was filled with it. Basically, I want a var set as true if everything worked. I have tried:

[CODE]
window.success = response;
[/CODE]

But it is always undefined, even when the script has been successful. I hope I have explained well.

Thanks.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@DokJul 29.2007 — Are you sure your XML request is being transmitted. Is the http variable in get_value() decalred global and where is it declared? The get_value() function should create a XMLHttpRequest object if none exist.
Copy linkTweet thisAlerts:
@Angry_Black_ManJul 30.2007 — your http.readyState being equal to four means you had a successful, finished response. i dont know anything about window.success and what youre trying to do with it, however.
Copy linkTweet thisAlerts:
@CreomireauthorJul 30.2007 — I was using window.success like this

<i>
</i>function handle_data()
{
if(http.readyState == 4)
{
var response = http.responseText;

<i> </i> if (response.length &gt; 0)
<i> </i> {window.success = true;}else{window.success = false}

<i> </i> document.getElementById(content_container).innerHTML = response;
<i> </i> document.getElementById(content_container).style.display = 'block';
<i> </i> // hide any elements if requiured
<i> </i> for (i = 0; i &lt; window.content_hide.length; i++)
<i> </i> {
<i> </i> document.getElementById(window.content_hide[i]).style.display = 'none';
<i> </i> }
<i> </i> // show any elements if requiured
<i> </i> for (i = 0; i &lt; window.content_show.length; i++)
<i> </i> {
<i> </i> document.getElementById(window.content_show[i]).style.display = 'block';
<i> </i> }
<i> </i>}
}


However, window.success is always showing as undefined and I know for sure that the http request was successful.
Copy linkTweet thisAlerts:
@CreomireauthorJul 30.2007 — By the way, I declared window success as a global variable at the start, outside all the functions:

[CODE]
var success = '';
[/CODE]
Copy linkTweet thisAlerts:
@CreomireauthorJul 30.2007 — Just to add,

It seems that the problem is that trying to update a global variable within the http.readystate condition doesn't work, but the global variable can be set outside it.

[CODE]
function handle_data()
{
/* declaring here would work */
if(http.readyState == 4)
{
/* declaring here doesn't seem to change the global var
}
}
[/CODE]
Copy linkTweet thisAlerts:
@Angry_Black_ManJul 30.2007 — unless you redeclare your "success" variable within the scope of the readystate checking function, this should work for you.
Copy linkTweet thisAlerts:
@CreomireauthorJul 30.2007 — unless you redeclare your "success" variable within the scope of the readystate checking function, this should work for you.[/QUOTE]

I am placing the exact same code where I show the comments in my last example. Outside the IF condition it works, inside it doesn't.

It's really confusing me.
Copy linkTweet thisAlerts:
@DokJul 30.2007 — Outside the IF condition it works, inside it doesn't.[/QUOTE]As I asked previously - are you sure that the condition is ever true?

Where are you declaring your http variable used in the get_value() function?

Can you verify that a XMLHttpRequest is indeed being created and sent?
Copy linkTweet thisAlerts:
@CreomireauthorJul 30.2007 — As I asked previously - are you sure that the condition is ever true?

Where are you declaring your http variable used in the get_value() function?

Can you verify that a XMLHttpRequest is indeed being created and sent?[/QUOTE]


Oh yeah, sure the request is being sent. I'm recieving data output back from the file called. The functions work perfectly in that respect, it's just that I can't configure a way to have the function that triggers all the others to be informed that everything went OK.

I mean, I can tell that everything went OK from my side, because I can see the output... but I want to invoke a failsafe system within the Javascript code, and therefore the calling function ...

get_value(req_url)

...has to know whether everything went OK.
Copy linkTweet thisAlerts:
@DokJul 30.2007 — Can you verify that http.readyState == 4 AND that http.status == 200 ?
Copy linkTweet thisAlerts:
@CreomireauthorJul 30.2007 — Just ran this code:

[CODE]
function handle_data()
{
if(http.readyState == 4)
{
var response = http.responseText;

// now update the document
document.getElementById(content_container).innerHTML = response;
document.getElementById(content_container).style.display = 'block';
// hide any elements if requiured
for (i = 0; i < window.content_hide.length; i++)
{
document.getElementById(window.content_hide[i]).style.display = 'none';
}
// show any elements if requiured
for (i = 0; i < window.content_show.length; i++)
{
document.getElementById(window.content_show[i]).style.display = 'block';
}
}

if(http.readyState == 4 && http.status == 200)
{
alert('yeah');
}
else
{
alert('nope');
}
}
[/CODE]


Three times I get "nope", and finally I get "yeah".

So, it runs through the 4 states, that's good right? So How could I get this to set a global variable that I can access later in the script?
×

Success!

Help @Creomire spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...