Jump to content

Archived

This topic is now archived and is closed to further replies.

Skewled

I'd like someone to verify my code :)

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Can you tell me which alerts to add where, I tried on my own but it just broke the code lol. Sorry

Share this post


Link to post
Share on other sites

in javascript:

alert('response: '+ request.responseText);

var hashed = hexMD5("okay");

alert('hashed: '+hashed);

var response = request.responseText;

alert(response.search(hashed));

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

what does the alert(response.search("saltokay")); return? that is the key.

Share this post


Link to post
Share on other sites

do you have any javascript errors showing?

 

also try

var req = responnse.search("saltokay");

alert('something: '+req);

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

function showUsernameStatus() { 

if (request.readyState == 4) {   

if (request.status == 200) {

var response = request.responseText;

var test = response.search("saltokay");

alert(test);

}

}

}

Share this post


Link to post
Share on other sites

Sends back 470 in the alert box, kept looping so I had to end the browser from the task manager

Share this post


Link to post
Share on other sites

hmm so the .search isnt working correctly...

 

so lets try this

 

var response = request.responseText;

var patt1=/saltokay/gi;

var ended = response.match(patt1);

if(endned != '') {

alert(ended);

}

 

Share this post


Link to post
Share on other sites

no alert and nothing occurs with the username input field

Share this post


Link to post
Share on other sites

function showUsernameStatus() { 

if (request.readyState == 4) {   

if (request.status == 200) {

var response = request.responseText;

var patt1 = "saltokay";

alert(response.match(patt1));

}

}

}

Share this post


Link to post
Share on other sites

that returned saltokay! I think your onto something now

Share this post


Link to post
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;

}

}

}

}

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×
×
  • 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.