Skewled Posted August 1, 2010 Share Posted August 1, 2010 I'm working on learning AJAX, all is fine with that script as it's from a book. The issue I'm having is the PHP server side code to get a response from the server. I'm still rather new to all of this so try to explain any response given in a noob friendly manner. So I have the following so far: // Connection Values $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Get the variable sent by AJAX script if (isset($_GET['username'])) { // Secure the information $passname = mysqli_real_escape_string($dbc, trim($_GET['username'])); // Lookup the username in the database $query = "SELECT username FROM information WHERE username = '$passname'"; $data = mysqli_query($dbc, $query); // Allow some time to get the response sleep(2); // If 1 is returned that name exsists, if 0 then we can move forward if (mysqli_num_rows($data) == 0) { // Send okay back to the ajax script so it knows to stop bothering the user echo 'okay'; } else { // Send denied back to the ajax script so the user knows that name is taken echo 'denied'; } } Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/ Share on other sites More sharing options...
Alex Posted August 1, 2010 Share Posted August 1, 2010 So what's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1093936 Share on other sites More sharing options...
Skewled Posted August 1, 2010 Author Share Posted August 1, 2010 The problem is that if the username isn't taken it still sends the user back to the input box rather then returning Okay and letting the user move forward in the registration process. Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1093941 Share on other sites More sharing options...
Skewled Posted August 2, 2010 Author Share Posted August 2, 2010 I guess the php code is correct for what I'm looking to do? Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1094197 Share on other sites More sharing options...
Alex Posted August 2, 2010 Share Posted August 2, 2010 Nothing jumps out at me initially, but why are you using sleep? That's unnecessary. What you should do is visit the page directly to see if it works. If it does then you know the problem is in your JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1094198 Share on other sites More sharing options...
radar Posted August 2, 2010 Share Posted August 2, 2010 the php code looks correct, which would mean the ajax code is the issue.. also ive noticed sometimes in my own ajax code that doing an echo won't always trigger the right response for my ajax.... so what i've been doing is <?php $ok = 'okay'; $den = 'denied'; if(something = something) { echo $ok; return $ok; } else { echo $den; return $den; } that was the trick for me, who knows Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1094199 Share on other sites More sharing options...
Skewled Posted August 2, 2010 Author Share Posted August 2, 2010 Thanks for the help! I'll give it a go with the method you mentioned radar. I was using the sleep() function to allow the user time to move onto another field in the form, and not keep them focused on the username field. I'll post my ajax code into the other forum and see if there is an issue with that. I hope not I just picked up the book to learn ajax lol. Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1094228 Share on other sites More sharing options...
Alex Posted August 2, 2010 Share Posted August 2, 2010 It would be a better idea to have the delay in the client-side JavaScript rather than having the server hang on the processing request. Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1094229 Share on other sites More sharing options...
Skewled Posted August 2, 2010 Author Share Posted August 2, 2010 I posted my ajax code into the ajax forum to be looked at here: http://www.phpfreaks.com/forums/index.php/topic,306208.0.html I'm just starting out with ajax and I was attempting to apply what I learned to my current registration form. If you guys know ajax also please feel free to look at that. I removed the sleep() and should learn how to do a delay in the javascript down the road. Quote Link to comment https://forums.phpfreaks.com/topic/209532-need-help-with-a-script-that-sends-information-back-to-ajax/#findComment-1094236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.