Jump to content

Need help with a script that sends information back to AJAX


Skewled

Recommended Posts

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.  ;D

 

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';
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.