unknown gamer Posted March 10, 2009 Share Posted March 10, 2009 Yes, I need help again, unfortunately... I was hoping this would be easy and thought about it and I figured this should work.Anyways, I have an input field for a username. Someone types in a username and ajax sends the username to a php file requesting if the username exists in the database. If it does it echo's "worked". So the ajax page recieves worked and the variable checks if the value is worked or something else... Apparantly it never equals worked. Here is the code:login.php: <html><head><meta http-equiv="content-type" content="text/html;charset=UTF-8"><title></title><script type="text/javascript">function my_ajax_post (data, url, callback, mode) {AJAX = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;AJAX.onreadystatechange = my_ajax_callback;AJAX.open ("post", url, mode);AJAX.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");AJAX.send (data);}function my_ajax_callback () {if (AJAX.readyState == 4){if (AJAX.status == 200) {if (AJAX.responseText == "worked"){document.getElementById("username").innerHTML="username worked";}else{document.getElementById("username").innerHTML="username does not exist";}} }}function username() {if (document.getElementById("pass").value == ""){var data = "username=";data += document.getElementById("user").value;my_ajax_post (data, "username.php", my_ajax_callback, true);}else{check();}}function check() {var data = "";my_ajax_post (data, "check.php", my_ajax_callback, true);}function password() {if (document.getElementById("user").value == ""){var data = "password=";data += document.getElementById("pass").value;my_ajax_post (data, "password.php", my_ajax_callback, true);}else{check();}}</script></head><body><?phpif (isset($_POST[submit])){include 'connect.php';$md5pass = md5($_POST['pass']);$result = mysql_query("SELECT * FROM bbg WHERE username='$_POST[user]' AND password='$md5pass'");if (mysql_num_rows($result) == 0){echo "Invalid login!"; exit;}else{while($row = mysql_fetch_array($result)){$expire=time()+60*60*24*30*12;setcookie("user", $_POST[user], $expire);echo $_POST[user];}}}?><div id="username"></div><div id="password"></div><form name="login" method="POST" action="">Username: <input type="text" id="user" name="user" onChange="username();" />Password: <input type="password" id="pass" name="pass" onChange="password();" /><input type="submit" name="submit" value="Login" /></form></body></html> username.php: <?phpinclude "connect.php";$result = mysql_query("SELECT * FROM bbg WHERE username='$_POST[username]'");if (mysql_num_rows($result) == 1){echo "worked";}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 Are you printing it out to see what it does equal? That would be a logical troubleshooting step. Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 What do you mean? on username.php, i am checking if the username entered on login.php is in the database, if it is it echoe's the text worked and on login.php i'm checking if the text echoed on username.php is equal to worked. Also for username.php I changed the POST to GET and tested it, it DOES echo worked. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 You wrote this: So the ajax page recieves worked and the variable checks if the value is worked or something else... Apparantly it never equals worked.So, what is it? Print out the AJAX response in Javascript and see what the value is. I mean, if you have a piece of code checking for a specific value, and it's never matching, and you don't know why, doesn't it make sense to see what value it actually is? Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 Well, yes, I did. I echoed it directly from username.php and it echoed exactly and ONLY worked. Now I set the div to the value returned in login.php and it still says worked. So I was curious and tried highlighting it I got "worked " with a space at the end... So I try that, no luck. Then I try the length code, i got a number of 299 or 293, i don't exactly remember. That really confused me and so I came here. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 You could try trimming the value, or maybe username.php has a bunch of whitespace around the PHP tags. There's no code in username.php that I see to output a 300-character string. Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 Well, that's exactly what username.php is. :/ Did you want to see a page showing what happens? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 If you have a test page, sure. Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 http://unknowngamer.net63.net/bbgtest/login.php use the username: unknowngamer any others won't work. the worked, is the response from the username.php page, the username does not exist is part of the if statement checking if the response is worked or not... Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 This is the response: worked <!-- www.000webhost.com Analytics Code --><script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script><noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript><!-- End Of Code --> That's why I say to check the response. Alerting that or using Firebug would have shown you what was going on. Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 I'm assuming that's the host, is there any way to work around that or fix it? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 In terms of removing it, you would have to ask the host. If you want to ignore it, you could split the response up on line breaks and check the first line. Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 I don't know how I could check the first line, could you tell me a code for the job? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2009 Share Posted March 10, 2009 The string.split method will split up a string on any character, such as a line break, and return an array with everything in it. The first element in the array will be the first line. You could also just limit the split to return only the first match.http://www.w3schools.com/jsref/jsref_split.asp Link to comment Share on other sites More sharing options...
unknown gamer Posted March 10, 2009 Author Share Posted March 10, 2009 Okay, its working, kind of. http://unknowngamer.net63.net/bbgtest/login.php when I enter a random name it works, when I refresh and enter unknowngamer it works. But i set it onChange call the function it used to work. Now I type in a fake name i get the message, type the real name and it never changes to username worked...Woops, had two variables the same, it works now. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.