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

Trouble with XMLHttpRequest in javascript

I'm trying to create a log out button that uses AJAX to call a php file which ends the current session:
Expand|Select|Wrap|Line Numbers
  1. //logout.php
  2. <?php
  3.     if (!session_start());
  4.     session_destroy(); //Destroys the session
  5.     echo "success";
  6. ?>
  7.  
When I put this in the function that calls logout.php, it says that there is a type mismatch on line 32 of doLogout.js (the script used to call logout.php):

Expand|Select|Wrap|Line Numbers
  1. //erroneous file
  2. var httpobj = null;
  3. function dothis()
  4. {
  5. }
  6. // Get the HTTP Object
  7. function obje(){
  8. if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  9. else if (window.XMLHttpRequest) return new XMLHttpRequest();
  10. else {
  11. alert("Your browser does not support AJAX.");
  12. return null;
  13. }
  14. }
  15. // Change the value of the outputText field
  16. function setthis(obj,link){
  17. if(httpobj.readyState == 4){
  18. if (httpobj.responseText=="success")
  19. {
  20.     document.getElementById(obj).innerHTML = "<a href='page.php?type=login' class='"+link+"'>Login</a>  |  <a href='page.php?type=register' class='"+link+"'>Register</a>";
  21. } else {
  22.     alert("Failed to log out!!!");
  23. }
  24. }}
  25. // Implement business logic
  26. function logout(obj,link){
  27. httpobj = obje();
  28. if (httpobj != null) {
  29. var parameters = "nothing=nothing";
  30. httpobj.open("GET", "ajax/logout.php", true);
  31. httpobj.send(parameters);
  32. httpobj.onreadystatechange = setthis(obj,link); //The ill-fated line 32.
  33. }}
  34.  
But when I put a simple 'alert();' above line 32, it works the exact way that I want it to, except for an annoying little prompt that pops up:

Expand|Select|Wrap|Line Numbers
  1. var httpobj = null;
  2. function dothis()
  3. {
  4. }
  5. // Get the HTTP Object
  6. function obje(){
  7. if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  8. else if (window.XMLHttpRequest) return new XMLHttpRequest();
  9. else {
  10. alert("Your browser does not support AJAX.");
  11. return null;
  12. }
  13. }
  14. // Change the value of the outputText field
  15. function setthis(obj,link){
  16. if(httpobj.readyState == 4){
  17. if (httpobj.responseText=="success")
  18. {
  19.     document.getElementById(obj).innerHTML = "<a href='page.php?type=login' class='"+link+"'>Login</a>  |  <a href='page.php?type=register' class='"+link+"'>Register</a>";
  20. } else {
  21.     alert("Failed to log out!!!");
  22. }
  23. }}
  24. // Implement business logic
  25. function logout(obj,link){
  26. httpobj = obje();
  27. if (httpobj != null) {
  28. var parameters = "nothing=nothing";
  29. httpobj.open("GET", "ajax/logout.php", true);
  30. httpobj.send(parameters);
  31. alert();//The annoying little prompt.
  32. httpobj.onreadystatechange = setthis(obj,link);//The ill-fated line 32.
  33. }}
  34.  
What happens when alert() is called that would make this work?
Basically what I'm tryin to have it do is when it is clicked, it passes its id and the id of it's parent node to a function which terminates the session and then changes the innerHTML of the parent node to a different link (a log in link - since they just logged out) which has the same css as the clicked link.
That's a mouthful, but it should be pretty simple. I've quintuple-checked this script and I can't find any errors or any reason for calling alert() to make it work.

Let me know what you figure out.

Gradinafrica
Jun 20 '09 #1
2 2425
Plater
7,872 Expert 4TB
the function you assign to the onreadystatechange cannot have parameters.

Example:
Expand|Select|Wrap|Line Numbers
  1. http.onreadystatechange = handleSendUpdateReponse;
  2.  
  3. function handleSendUpdateReponse()
  4.  
I think you can get away with doing:
Expand|Select|Wrap|Line Numbers
  1. httpobj.onreadystatechange = function{setthis(obj,link);}
  2.  
See if that helps you
Jun 22 '09 #2
Dormilich
8,658 Expert Mod 8TB
should be rather
Expand|Select|Wrap|Line Numbers
  1. httpobj.onreadystatechange = function() { setthis(obj,link); }
or (which is essentially the same)
Expand|Select|Wrap|Line Numbers
  1. var tempFn = function() { setthis(obj,link); }
  2. httpobj.onreadystatechange = tempFn;
Jun 22 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mr_burns | last post by:
Hi there, I am using the following function to import a xml file whether the users browser be IE or Mozilla: function importXML(file) { var xmlDoc; var moz = (typeof document.implementation...
20
by: Gaz | last post by:
In Internet Explorer 6 I'm having a problem with the httprequest object. I use it to call a webservice and display the result in the readystate event handler. This works the first time I call it...
21
by: Joe Attardi | last post by:
Hey all! I was reading over at the IE Blog the other day http://http://blogs.msdn.com/ie/] and read some interesting, and encouraging news. According to Sunava Dutta, an IE Program Manager,...
1
by: vocalise | last post by:
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed...
5
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
1
by: Tarik Monem | last post by:
OK, I'm pretty sure this cannot work because I'm trying to use JavaScript (client-side) to write to an xml file (which is server-side) using XMLHttpRequest. Can I use PHP do what I'm trying to do?...
4
Plater
by: Plater | last post by:
I have a simple page that performs a simple request on a 5second interval. It requests some data from a script I have, and then parses and updates the page accordingly. Simple. I am pretty sure my...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
1
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...
0
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...
0
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...

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.