Jump to content

I'd like someone to verify my code :)


Skewled

Recommended Posts

Everything appears to work, I've added alert's to each section and got the required answers in the browser, but for some reason when my PHP script checks the username to see if it's been taken it will return taken no matter what. I posted my PHP code into the php forum here http://www.phpfreaks.com/forums/index.php/topic,306140.0.html , and everything checks out with that. So I'm moving onto my next step to see if the code I compiled below is functioning properly.

 

 

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

 

I have the browser check called from another script, this is called before the script above is loaded.

 

function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (tryMS) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (otherMS) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }	
  return request;
}

Link to comment
Share on other sites

  • Replies 69
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

remove this:

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

 

and replace it temporarily with alert(request.responstText); and see what it displays.

Link to comment
Share on other sites

so changing:

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

      }

    }

  }

}[/code]

 

to

function showUsernameStatus() {
if(request.readyState == 4) {
if(request.status == 200) {
	alert(request.responseText);
}
}
}

 

does nothing?

Link to comment
Share on other sites

also change your one function to this:

 

function createRequest()
{
var req;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	req = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
			return false;
		}
	}
}

return req;
}

 

then

if (request == null) to

if(request) {

// stuff here

} else {

// errors here

}

Link to comment
Share on other sites

Made those changes and now get "unable to create request"

 

here's what I have now for my utility scritp:

 

function createRequest()
{
   var req;  // The variable that makes Ajax possible!
   
   try{
      // Opera 8.0+, Firefox, Safari
      req = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }

   return req;
}

 

And now the new validation script:

 

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)
    alert("Unable to create request");
  else {
    var theName = document.getElementById("username").value;
    var username = escape(theName);
    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);
    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;
      }
    }
  }
}

Link to comment
Share on other sites

  // Function to actually check the username and what to do with it
function checkUsername() {
	request=createRequest();
  if (request) {
  	var theName = document.getElementById("username").value;
    var username = escape(theName);
    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);
    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) {
       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;
      }
    }
  }
}

Link to comment
Share on other sites

That got rid of the nasty error and I see what I did wrong, ty for pointing out the new code.

 

I removed the delay from the php file so it updates instantly now, the issue is still in the showUsernameStatus function.

 

For some reason it's not getting back an "okay" or "denied" from the server, that's why I posted my PHP code first.

 

Any suggestions? ty again for all your help thus far

Link to comment
Share on other sites

okay since it's not gettin an okay in the responseText, lets see what exactly we're getting...  visit the .php page that is to be generating this result, and see what type of a result it has.

 

also are you using both echo and return?

Link to comment
Share on other sites

The php page when accessed directly with signup.php?username=taken

 

taken is already a username that's used

 

it returns denied like it should

 

Also if it name is available it returns okay

 

I'm using echo and return as well

Link to comment
Share on other sites

what that means, the SQL isnt really parsing...

 

so what i would do is this:

 

$sql = "SELECT whatever";

echo $sql;

$data = mysqli_query($sql);

 

then see what the output is of the echo on $sql, that'll tell you what the issue is with the query.  once you get that working right, the whole script will work right.

 

-- okay so it runs denied...  how bout if you put in signup.php?username=ucntkilme

 

how are you handling the usage of username in the php file?

Link to comment
Share on other sites

I modified my above post because I had an error in the query that I just fixed.

 

So that seems to be working fine, but the issue is the ajax isn't getting the server response back for some reason.

 

var url= "signup.php?username=" +username;

 

this passes the variable that's entered so the php page would see

 

signup.php?username=enteredname

 

when I do it manually it comes back okay or denied with the echo output.

Link to comment
Share on other sites

do you have the formfield 'username' with the id="username" in it?

 

also through 3 alerts up..

 

one for document.getelementbyID('username').value

one for theName

and one for username and lets see what shows up in all 3.

Link to comment
Share on other sites

if nothing shows up in username, issue is the document.getElementByID so we'd then do something like this instead

 

var theName = window.document.formname.username.value;

 

you could also use:

 

var theName=$('username').value;

Link to comment
Share on other sites

I did the alerts here they are:

 

1st - Original value: Taken

                That's the name I entered into the username box

 

2nd - Escaped value: Taken

             

3rd - Url - ../signup?username=Taken

 

4th - Sorry, that username is taken.

 

That username isn't registered and shouldn't have put up the last error.

Link to comment
Share on other sites

I just went to the other post and reviewed your php code, just for shits and giggles change your $_GET to $_REQUEST

 

and echo out $query and see what returns.

 

oh and Taken == taken

 

if you want usernames to be case sensitive you need to use WHERE username LIKE BINARY $_REQUEST['passname'] instead of WHERE username =

Link to comment
Share on other sites

ok I changed it to $_REQUEST and nothing seemed to change

 

I then echo'd out the $query and nothing seemed to change

 

I didn't see the small page 2 at the bottom my apologies lol

 

The query is setup so that $_REQUEST['username'] is passed to a new variable if it's set.

 

  if (isset($_REQUEST['username'])) {

 

then I escape and trim the new variable and throw it into the query for the WHERE clause ( username = '$passname'";

 

$passname = mysqli_real_escape_string($dbc, trim($_GET['username']));

$query = "SELECT username FROM information WHERE username = '$passname'";
$data = mysqli_query($dbc, $query);

$ok = 'okay';
$den = 'denied';

if (mysqli_num_rows($data) == 0) {
  // if the returned value is 0 then the username is unique
    echo 'okay';
    return $ok;
} else {
   // that username is taken so let's deny it
   echo 'denied';
   return $den;
}

Link to comment
Share on other sites

k change that to this temporarily..

 

$pass = isset($_REQUEST['username']) ? $_REQUEST['username'] : '';

if($pass != '') {

$passname = mysqli_real_escape_string($dbc, trim($_REQUEST['username']));

$query = "SELECT COUNT(username) FROM information WHERE username = '".$passname."'";

echo $query;

$data = mysqli_query($dbc, $query);

$data = mysqli_result($data, 0);

$ok = 'okay';

$den = 'denied';

 

if($data != '' && $data >= 1) {

echo $ok;

return $ok;

} else {

echo $den;

return $den;

}

}

}

 

and see what that does.

Link to comment
Share on other sites

 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
     <label for="username">Username:</label>
     <input type="text" id="username" name="username value="<?php if (!empty($username)) echo $username; ?>" MAXLENGTH = 7 /><br />
<label for"register"></label>
    <input id="register" type="submit" value="Sign Up" name="register" class="signsub" />
  </form>

 

This is what I'm currently working with, I'll be adding password, e-mail and other stuff after this.

Link to comment
Share on other sites

  <input type="text" id="username" name="username value="<?php if (!empty($username)) echo $username; ?>" MAXLENGTH = 7 /><br />

 

to

  <input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" MAXLENGTH = 7 /><br />

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.