Jump to content

I'd like someone to verify my code :)


Skewled

Recommended Posts

Everything runs smoothly and it get's to the alert for request.responseText and displays the first half of the page then the MD5 for okay or denied. It's essentially doing the same thing as before.

 

Here's the new codes just to keep on the same page:

 

Java:

window.onload = initPage;

function initPage() {
  // Tell the browser what element we need to work with and what function to call
  document.getElementById("username").onblur = checkUsername;
  document.getElementById("register").disabled = true;
}

  // Function to actually check the username and what to do with it
function checkUsername() {
    request=createRequest();
  if (request) {
    var theName = document.getElementById("username").value;
alert("Original name value: " + theName);
     var username = escape(theName);
alert("Escaped name value: " + username);
    var url= "signup.php?username=" + username;
   // Every time the ready state of the response changes it will update the browswer
    request.onreadystatechange = showUsernameStatus;
    request.open("GET", url, true);
alert("URL: " + url);
    request.send();
   
  } else {
    alert("Unable to create request");
  }
}

  // Update the page after the browser gets a response from the server
function showUsernameStatus() {
  if (request.readyState == 4) {
    if (request.status == 200) {
alert(request.responseText);
var hashed = hexMD5('okay');
var response = request.responseText;
if(response.search(hashed) != -1) {
       document.getElementById("username").className = "approved";
//       document.getElementById("register").disabled = false;
      } else {
alert("Sorry, that username is taken.");
       document.getElementById("username").className = "denied";
       document.getElementById("username").focus();
       document.getElementById("username").select();
       document.getElementById("register").disabled = true;
      }
    }
  }
}

 

PHP:

 

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

   // Get the variable sent by AJAX script if it is typed in
if (isset($_REQUEST['username'])) {
$ok = md5("okay");
$den = md5("denied");

   // Secure the information
$passname = mysqli_real_escape_string($dbc, trim($_REQUEST['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 $ok;
return $ok;
} else {
   // Send denied back to the ajax script so the user knows that name is taken
echo $den;
return $den;
}
  }

Link to comment
Share on other sites

  • Replies 69
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Okay so it looks like the code is good...

 

just to test the two md5 hashes...

 

add an alert for hashed, and add one for request.search(hashed) and see what all 3 of them say...

 

this way, we can determine that javascript is hashing the word 'okay' in the same way that php does it..  and if it does, we'll know exactly what number to look for.

Link to comment
Share on other sites

md5 okay = df8fede7ff71608e24a5576326e41c75

md5 denied = 865726b2885feef8e8b25b56a2d7c8f8

 

both alert hashes didn't pop up a dialog box

the alert('response: '+ request.responseText); popped up with the md5 values I'll paste it here.

 

response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>User Registration</title>

<link type="text/css" rel="stylesheet" href="tlstyle.css" />

<script src="scripts/utils.js" type ="text/javascript"></script>

<script src="scripts/validation.js" type="text/javascript"></script>

</head>

<body>

<div id="csignup">


865726b2885feef8e8b25b56a2d7c8f8

Link to comment
Share on other sites

hmmm..  okay...  so lets change it to this just to test it's working...

 

in php:

 

$ok = 'saltokay';

$den = 'saltdenied';

 

in javascript:

 

var response = request.responseText;

alert(response.search("saltokay"));

 

and lets see what that does.

Link to comment
Share on other sites

response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>User Registration</title>

<link type="text/css" rel="stylesheet" href="tlstyle.css" />

<script src="scripts/utils.js" type ="text/javascript"></script>

<script src="scripts/validation.js" type="text/javascript"></script>

</head>

<body>

<div id="csignup">


saltokay

 

Looks like it's returning the correct value but the code above it won't go away and I don't know how to change that.

Link to comment
Share on other sites

Sorry I got abit confused, I reverted the code back to normal. PHP is still the same but here's the javascript:

 

window.onload = initPage;

function initPage() {
  // Tell the browser what element we need to work with and what function to call
  document.getElementById("username").onblur = checkUsername;
  document.getElementById("register").disabled = true;
}

  // Function to actually check the username and what to do with it
function checkUsername() {
  document.getElementById("username").className = "thinking";
  request = createRequest();
  if (request == null)
    alert("Unable to create request");
  else {
    var theName = document.getElementById("username").value;
    var username = escape(theName);
    var url= "signup.php?username=" + username;
   // Ever time the ready state of the response changes it will update the browswer
    request.onreadystatechange = showUsernameStatus;
    request.open("GET", url, true);
    request.send(null);
  }
}

  // Update the page after the browser gets a response from the server
function showUsernameStatus() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      if (request.responseText == "okay") {
        document.getElementById("username").className = "approved";
        document.getElementById("register").disabled = false;
      } else {
        document.getElementById("username").className = "denied";
        document.getElementById("username").focus();
        document.getElementById("username").select();
        document.getElementById("register").disabled = true;
      }
    }
  }
}

 

Let me know what you'd like me to change

Link to comment
Share on other sites

okay so

 

function showUsernameStatus() {

if(request.readyState == 4) {

if(request.status == 200) {

var response = request.responseText;

var patt1 = "saltokay";

var response = response.match(patt1);

if (response == 'saltokay') {

document.getElementById("username").className = "approved";       

document.getElementById("register").disabled = false;

} else {

document.getElementById("username").className = "denied";        document.getElementById("username").focus();        document.getElementById("username").select();        document.getElementById("register").disabled = true;

}

}

}

}

Link to comment
Share on other sites

You sir rock!

 

Just  to make sure I understand what the issue was.

 

1 PHP was returning the proper response

2 AJAX code was not accounting for the begining of the document text to the bottom result

3 You used var response = request.responseText; to place the response into a variable

4 You then used var patt1 = "saltokay"; to establish the value we were looking for

5 You then used var response = response.match(patt1); to search the text sent from PHP to AJAX to limit the response to just "saltokay"

6 Changed the initial IF statement to if(response == "saltokay") {

  because we no longer needed to check the entire responseText

 

I just want to make sure I was learning as you were troubleshooting and helping me.

 

I guess the book I'm using must be outdated or expected the PHP script to only return a value and the form on a seperate page? I might have to send that book back if it's the case lol.

 

Thanks!!

 

I tested a valid name and an invalid name and both worked properly.

Link to comment
Share on other sites

PHP was returning the response, just not by itself it had HTML attached to it.

 

this made it so that the way we were checking, AJAX couldnt read 'okay'

 

3 is correct

4 is also correct

5 is sorta correct, var response = response.match(patt1); will actually return all elements of 'saltokay' into the variable.  this is why we used a word combination that hopefully nobody will be using in an input box.

 

6 is correct.

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.