30 Replies - 5454 Views - Last Post: 02 December 2012 - 04:30 PM
#16
Re: AJAX & PHP login system not working
Posted 27 November 2012 - 12:01 PM
#17
Re: AJAX & PHP login system not working
Posted 28 November 2012 - 08:07 AM
Thank you!
-Tim
#18
Re: AJAX & PHP login system not working
Posted 28 November 2012 - 08:11 AM
#19
Re: AJAX & PHP login system not working
Posted 28 November 2012 - 09:03 AM
Thank you.
-Tim
#20
Re: AJAX & PHP login system not working
Posted 28 November 2012 - 10:14 AM
So, everyone else has it working but you don't, and that leads you to thinking that you're surely doing it the right way, while advice from them is only making your code worse? Huh?
Maybe we should try again then. Post your code (updated).
#21
Re: AJAX & PHP login system not working
Posted 28 November 2012 - 02:59 PM
Xupicor, on 28 November 2012 - 05:14 PM, said:
So, everyone else has it working but you don't, and that leads you to thinking that you're surely doing it the right way, while advice from them is only making your code worse? Huh?
Maybe we should try again then. Post your code (updated).
Hey no need to be rude, I am just explaining the results I get from my code when I update it with the suggestions that you have made. I am not saying that I am doing it the right way, obviously I am doing something wrong, I just did not get any better results when applying the fixes you mentioned. This is the code that works for me right now:
function check_login() { console.log("In check_login()"); if(dataTransfer.readyState == 4 || dataTransfer.readyState == 0) { dataTransfer.open("POST", 'php/login.php', false); dataTransfer.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var param = "username=" + document.getElementById("login_username_input").value; param += "&password=" + document.getElementById("login_password_input").value; console.log(param); dataTransfer.send(param); dataTransfer.onreadystatechange = handleDataTransferLogin(); } } function handleDataTransferLogin() { if(dataTransfer.readyState == 4) { console.log("Response: " + dataTransfer.responseText); document.getElementById("status").innerHTML = dataTransfer.responseText; } }
I had to make it syncronous to make it work the way I wanted. But ok, I'll take you through the changes I made and the results I got.
1)
Dormilich, on 27 November 2012 - 05:33 PM, said:
I changed the dataTransfer.onreadystatechange = handleDataTransferLogin(); to dataTransfer.onreadystatechange = handleDataTransferLogin;
This just made the code not run even after I pressed the Enter button twice. This is because the dataTransver.readyState never reached 4. But when I did it as the code was initially the dataTransfer.readyState was equal to 4 at the start of the function.Well, this is not really true. The readyState did equal to 4 on the secound time I pressed the button after I made the changes too, but then the dataTransfer.onreadystatechange = handleDataTransferLogin; did not fire.
2)
Dormilich, on 27 November 2012 - 06:27 PM, said:
tim9009, on 27 November 2012 - 07:13 PM, said:
if you put () behind a function name you will execute the function no matter what. whether the function does what it should and at the right time is another matter.
tim9009, on 27 November 2012 - 07:13 PM, said:
naturally, the default value of the XMLHttpRequest object’s readyState is not 4.
tim9009, on 27 November 2012 - 07:13 PM, said:
what you get is the result of the first AJAX request, not the one triggered by the second button press (that they do not differ just hides this fact).
in short, your problem is the execution time. you execute the handler function before you run the request, hence you have no result. and when you press the button a second time, you run the handler function again (before the request), but thanks to the global variable of the request, the data from the first request are still there and you can process them. and then you fire a second AJAX request, which you leave unprocessed.
This all made sense and I tried to play around with having the dataTransfer.onreadystatechange = handleDataTransferLogin; at different places in the function, and also outside of the function. The only results I got was that the readyState never went past 1 and that I never got any results back from the php file when I tried anything but the initial way and the final way that I posted above. There might be something that I have not tried or done wrong here so feel free to give me some hits as to what it might be.
What I found when doing it the fixed way (dataTransfer.onreadystatechange = handleDataTransferLogin;) was that the dataTransfer did get to readyState 4 the secound time that I run the check_login function, but the code never got to the handleDataTransferLogin eaven though the readyState changed to 4.
I would really appreciate some feedback on the inforation I have given here, and thank you all so much for helping out! As I have made a fix already time is not really a problem any more, but I would really like to figure out what I am doing wrong so that I may fix it another time.
Thank you!
-Tim
#22
Re: AJAX & PHP login system not working
Posted 28 November 2012 - 11:10 PM
#23
Re: AJAX & PHP login system not working
Posted 29 November 2012 - 12:27 AM

If you make changes Dormilich suggested it should work. If not, get back to us.
I bit more on them - at the moment you're sending request first, without any handler set. You want to set handler first, and then fire up the ajax call.
Now, about parenthesis - when you use them you call your function.
var foo = hello();this calls hello() and assigns its return value to variable foo. A common usage, but this is not what you want here.
var foo = hello;this does not call the function, it assigns it to variable foo. Now you can do this somewhere else in the code:
foo()and it will call hello().
This is exactly what you want, you want to pass function as a handler, and then when the event happens your handler will be called for you. Does that make sense?
edit: You may also want to check out .status value.
This post has been edited by Xupicor: 29 November 2012 - 12:30 AM
#24
Re: AJAX & PHP login system not working
Posted 30 November 2012 - 09:54 AM
I tried what you are suggesting there about having the handler before the send and it does make sanse and sound as the right way to do it, but when I tried it it did not work because the AJAX call never reach readyState 4 for some reason. But when I write it the way that works for me now it will reach readyState 4. This is strange as I don't really change any of the code that directly has something to do with the AJAX call. I will try to re-write the code again to cehck if I did something wrong the last time.
Thank you!
-Tim
This post has been edited by Dormilich: 30 November 2012 - 12:10 PM
Reason for edit:: removed unnecessary quote
#25
Re: AJAX & PHP login system not working
Posted 01 December 2012 - 01:09 AM

#26
Re: AJAX & PHP login system not working
Posted 01 December 2012 - 08:17 PM
I just found the problem! What's funny is that it was somewhat figured out in the first reply.
onreadystatechange
Had to be changed to:
onreadystatechange
It was as simple as that. Ever notice how the issues that you use the most time figuring out are the really simple ones?

-Tim
[edit]
Well the text changed for some reason... the first code example should have a capital letter in front of each word. That might be why it has taken so long to figure out. Only the word onreadystatechange turns in to all lower case letters when typed inn here. Strange.
This post has been edited by tim9009: 01 December 2012 - 08:21 PM
#27
Re: AJAX & PHP login system not working
Posted 02 December 2012 - 02:54 AM
This post has been edited by Dormilich: 02 December 2012 - 02:57 AM
#28
Re: AJAX & PHP login system not working
Posted 02 December 2012 - 08:16 AM
Dormilich, on 02 December 2012 - 09:54 AM, said:
Yes I know that, I'm just saying that it is strange that dreamincode changes what I wrote to all lower case letters. That is kind of stupid as this problem would have been solved much faster had it not been changed.
-Tim
#29
Re: AJAX & PHP login system not working
Posted 02 December 2012 - 09:26 AM
Thank you for taking the time to write to us about it tim9009. I for one was not aware that DIC has one of its main if not main facility, THAT much broken.
#30
Re: AJAX & PHP login system not working
Posted 02 December 2012 - 11:34 AM
Xupicor, on 02 December 2012 - 04:26 PM, said:
Thank you for taking the time to write to us about it tim9009. I for one was not aware that DIC has one of its main if not main facility, THAT much broken.
No problem, i'm just happy to help.
-Tim