AJAX & PHP login system not working

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2570 Views - Last Post: 02 December 2012 - 04:30 PM

#16 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: AJAX & PHP login system not working

Posted 27 November 2012 - 12:01 PM

yupp. the problem is that the request is triggered inside the handler, which is executed once the request returns (that never started Ö). lines #13+ have to be outside the readystate handler.
Was This Post Helpful? 0
  • +
  • -

#17 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 28 November 2012 - 08:07 AM

This problem is still weird... I tried all of the things you guys said and still it did not work. I could get it to work by making the third parameter in the AJAX call false (syncronous) and then placing the onreadystatechange after the send(). This made the data show correctly right away. I would also get a close to correct system by setting the AJAX back to asyncronous, the onreadystatechange back to the top and then adding a timeout loop in the function that is to write the data to the screen. It would then write the responseText correctly on the secound loop. This is basically the same as me pressing the enter button twice. I still don't think that any of theese "fixes" are the best one, but atleast it is somewhat working. If you have any other ideas for what I could change to make it work then please share them.

Thank you!

-Tim
Was This Post Helpful? 0
  • +
  • -

#18 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: AJAX & PHP login system not working

Posted 28 November 2012 - 08:11 AM

View Posttim9009, on 28 November 2012 - 04:07 PM, said:

If you have any other ideas for what I could change to make it work then please share them.

I would recommend to write the AJAX code like everyone else. from your description you seem to still have the same type of code (with the same type of problems).
Was This Post Helpful? 1
  • +
  • -

#19 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 28 November 2012 - 09:03 AM

I HAVE written the code like everyone else. I have looked at countless tutorials and sites and I have done the same thing only to find that it does not work in my code. I don't know what more to tell you. All the suggestions you guys have made have only made the code worse. I am not saying that you are giving bad advice (I see a lot of tutorials doing it the same way) but it does not work when I try to do it.

Thank you.
-Tim
Was This Post Helpful? 0
  • +
  • -

#20 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: AJAX & PHP login system not working

Posted 28 November 2012 - 10:14 AM

"All" the suggestions? You were calling the handler function where you should have passed it (the function, not its return value). Surely fixing that didn't make your code worse.

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).
Was This Post Helpful? 0
  • +
  • -

#21 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 28 November 2012 - 02:59 PM

View PostXupicor, on 28 November 2012 - 05:14 PM, said:

"All" the suggestions? You were calling the handler function where you should have passed it (the function, not its return value). Surely fixing that didn't make your code worse.

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)

View PostDormilich, on 27 November 2012 - 05:33 PM, said:

rule: event properties (.onclick, .onreadystatechange, etc.) expect function names to be given, not executing functions.


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)

View PostDormilich, on 27 November 2012 - 06:27 PM, said:

View Posttim9009, on 27 November 2012 - 07:13 PM, said:

The thing is that handleRecieveData IS being triggered in the initial code that I gave you. That is what I have been trying to explain.

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.


View Posttim9009, on 27 November 2012 - 07:13 PM, said:

The issue is that it does not go past the if statement on line 21 before I run it a scound time

naturally, the default value of the XMLHttpRequest objectís readyState is not 4.


View Posttim9009, on 27 November 2012 - 07:13 PM, said:

before I run it a scound time eaven though I can access the returnText and get the correct result through console in chrome.

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
Was This Post Helpful? 0
  • +
  • -

#22 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: AJAX & PHP login system not working

Posted 28 November 2012 - 11:10 PM

line #13 should be before line #12 and still without the (). (what good is an event handler, if you assign it after the event occurs?)
Was This Post Helpful? 1
  • +
  • -

#23 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: AJAX & PHP login system not working

Posted 29 November 2012 - 12:27 AM

I wasn't trying to be rude, I just repeated after you. I did think that your post was a little rude though. Still, no need to get excited, we all have our good and bad days, and since text doesn't really send out emotions that well misunderstandings are bound to happen. ;)/>

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

Was This Post Helpful? 1
  • +
  • -

#24 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 30 November 2012 - 09:54 AM

Yeah, I am sorry.

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

Was This Post Helpful? 0
  • +
  • -

#25 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: AJAX & PHP login system not working

Posted 01 December 2012 - 01:09 AM

You could try to debug it - log with what readyStates your handler is being called, with what status, etc. With that "double call" you can get into trouble if you use similar code outside of login script. I mean, what if you need to send new post/comment request twice? You get double post/comment. You can imagine more of scary examples. ;)
Was This Post Helpful? 1
  • +
  • -

#26 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 01 December 2012 - 08:17 PM

Hello

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? :P But anyway, thank you all so much for the help. I have learned a few things I did not know so it was all worth it. Again: thank you!

-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

Was This Post Helpful? 0
  • +
  • -

#27 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: AJAX & PHP login system not working

Posted 02 December 2012 - 02:54 AM

that’s not strange, that’s called case-sensitivity. .onreadystatechange is different from .on​ReadyStateChange (etc.).

This post has been edited by Dormilich: 02 December 2012 - 02:57 AM

Was This Post Helpful? 0
  • +
  • -

#28 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 02 December 2012 - 08:16 AM

View PostDormilich, on 02 December 2012 - 09:54 AM, said:

thatís not strange, thatís called case-sensitivity. .onreadystatechange is different from .on​ReadyStateChange (etc.).


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
Was This Post Helpful? 1
  • +
  • -

#29 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: AJAX & PHP login system not working

Posted 02 December 2012 - 09:26 AM

It's not "kind of stupid" - it's really very stupid. We really need to do something about it people.

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.
Was This Post Helpful? 1
  • +
  • -

#30 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: AJAX & PHP login system not working

Posted 02 December 2012 - 11:34 AM

View PostXupicor, on 02 December 2012 - 04:26 PM, said:

It's not "kind of stupid" - it's really very stupid. We really need to do something about it people.

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
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3