/    Sign up×
Community /Pin to ProfileBookmark

XMLHttpRequest Error

I’m getting an odd error, and I can’t quite figure out why.

[URL=”http://localhost/motorpro/admin/error.php”]Site Is Here[/URL]

I’ve got a page that will update a database, either changing the number of participants in a class or adding another class entirely.

Both versions use the same XMLHttpRequest function to call another PHP file to update or add the information to the MySQL database.

However, while the form which updates the number of participants in a class works fine, the one to add a class does not, with Firefox Error Console telling me “xmlhttp is undefined”.

[CODE]
var xmlhttp;

function load_external(url,run_name)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=run_name;
xmlhttp.open(“GET”,url,true);
xmlhttp.send(null);
}
else
{
alert(“Your browser does not support this function.”);
}
}

function updateCurrentClasses(date,spaces) {
infoURL = “demo.php”;
load_external(infoURL,update_classes);
}
function update_classes() {
if (xmlhttp.readyState==4)
{// 4 = “loaded”
if (xmlhttp.status==200)
{// 200 = OK
alert(“Number of spaces available updated.”);
window.location.reload(true);
}
else
{
alert(“Problem retrieving XML data”);
}
}
}

function addClass(date,spaces) {
longdate = longDate(date.value);
reformatdate = reformatDate(date.value);
infoURL = “demo.php”;
load_external(infoURL,add_classes(longdate,spaces));
}
function add_classes(longdate,spaces) {
if (xmlhttp.readyState==4)
{// 4 = “loaded”
if (xmlhttp.status==200)
{// 200 = OK
alert(“Class for ” + longdate + ” has been added with ” + (spaces.selectedIndex+1) + ” spaces.”);
window.location.reload(true);
}
else
{
alert(“Problem retrieving XML data”);
}
}
}
[/CODE]

The updateCurrentClasses function performs as it is supposed to. The addClass function does not, although they are almost identical.

Any ideas?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineApr 02.2009 — 
The updateCurrentClasses function performs as it is supposed to. The addClass function does not, although they are almost identical.


[CODE]load_external(infoURL,add_classes(longdate,spaces));[/CODE]

[/QUOTE]
The second parameter is supposed to be a function address.
Copy linkTweet thisAlerts:
@xclntdesignauthorApr 02.2009 — It is. add_classes(longdate,spaces) is the function.

The script gets to the add_classes function, but seems to forget the xmlhttp variable.
Copy linkTweet thisAlerts:
@ZeroKilledApr 02.2009 — i guess you don't understood what [b]sterlin[/b] tried to tell you. what s/he meant was that you have to pass the function, not to call it. when a function name is followed with parenthesis javascript interpret it as a function call. an example of it is this line
load_external(infoURL,[b]update_classes[/b]);
notice update_classes isn't called, instead is passed as an argument. however, in your case you have to create an anonymous function due that you have to pass some arguments:

<i>
</i>load_external(infoURL,function(){add_classes(longdate,spaces)});
Copy linkTweet thisAlerts:
@xclntdesignauthorApr 02.2009 — 1,000 thanks!

Sorry I didn't quite understand that, it's been a long day!
×

Success!

Help @xclntdesign 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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