Jump to content

dynamic username check


lee2010

Recommended Posts

Hi all, i have a script which checks the username field of my register form as the user types it and checks to see if the username is either taken, is too short or availible using jquery, however it isn't working and i've been staring at it for ages trying to work out why.

 

Due to the length of my code ive put it on codepad.

 

Here is my register.php: http://codepad.org/8REOfI8q

 

and here is my check.php: http://codepad.org/gXSkbnsf

 

My form is just displaying "Choose a username" and not changing depending on my input etc...

 

Any help would be great

Link to comment
Share on other sites

sure, here we go

 

check.php:

<?php

mysql_connect("localhost", "lee", "leecopter");
mysql_select_db("lee");

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT username FROM users WHERE username='$username'");
$check_num_rows = mysql_num_rows($check);

if ($username==NULL)
echo "Choose a username";

else if (strlen($username)<=3)
echo "Username is too short";

else 
{
if ($check_num_rows==0)
echo "Username is availible!";

else if ($check_num_rows==1)
echo "Username has already been taken";
}
?>

 

register.php:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!--
Author: Reality Software
Website: http://www.realitysoftware.ca
Note: This is a free template released under the Creative Commons Attribution 3.0 license, 
which means you can use it in any way you want provided you keep the link to the author intact.
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$('#feedback').load('check.php').show();

$('#username_input').keyup(function(){
	$.post('check.php', { username: register.username.value},
	function(result) {
	$('#feedback').html(result).show();
	});
});

});

</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.auto-style2 {
font-size: 25px;
}
.auto-style3 {
font-size: small;
}
</style>
</head>
<body>

<div id="container">
<!-- header -->
    <div id="header">
    	<div id="logo"><a href="#"><span class="orange">SouthWest</span> LAN's</a></div>
        <div id="menu">
        	<ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="events.php">Events</a></li>
            <li><a href="account.php">My Account</a></li>
            <li><a href="forum.php">Forums</a></li>
            <li><a href="faq.php">FAQ</a></li>
            </ul>
        </div>
    </div>
    <!--end header -->
    <!-- main -->
    <div id="main">
        <div id="content">
        <div id="head_image">
        	<div id="slogan"><strong><span class="auto-style2">Organising LAN Parties in the SouthWest</span></strong><br /></div>
            <div id="under_slogan_text"></div>
        </div>
        <div id="text">
          <h1>Registration:</h1>
           <br />
          
           <p class="auto-style3">Don't forget your login details as you will need them everytime you wish to login.</p>
	   
<form name="register" method="post" action="process_r.php">
<table border="0" width="225" align="center">
    <tr>
        <td width="219" bgcolor="#999999">
            <p align="center"><font color="white"><span style="font-size:12pt;">
		All fields are required</span></font></p>
        </td>
    </tr>
    <tr>
        <td width="219">
            <table border="0" width="282" align="center">
                    <tr>
                        <td width="116"><span style="font-size:10pt;">Username:</span></td>
                        <td width="156"><input type="text" id="username_input" name="username" maxlength="100"></td>
                    </tr>
				<tr>
                        <td width="116"><span style="font-size:10pt;"></span></td>
                        <td width="116"><div id="feedback"></div></td>
                    </tr>
                    <tr>
                        <td width="116"><span style="font-size:10pt;">Email:</span></td>
                        <td width="156"><input type="text" name="email" maxlength="100"></td>
                    </tr>
                <tr>
                    <td width="116"><span style="font-size:10pt;">Password</span></td>
                    <td width="156"><input type="password" name="password"></td>
                </tr>
                <tr>
                    <td width="116"><span style="font-size:10pt;">Confirm Password:</span></td>
                    <td width="156"><input type="password" name="c_password"></td>
                </tr>
                <tr>
                    <td width="116"> </td>
                        <td width="156">
                            <p align="right"><input type="submit" name="register" value="Register"></p>
                        </td>
                </tr>
            </table>
        </td>
    </tr>
    
</table>
</form>

                       </div>
              </div>
       </div>
    </div>
    <!-- end main -->
    <!-- footer -->
    <div id="footer">
    <div id="left_footer"><a href="http://validator.w3.org/check?uri=referer"><img
        src="http://www.w3.org/Icons/valid-xhtml10"
        alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a><br />© Copyright 2008 Clicker design
    
  

    </div>
    <div id="right_footer">

<!-- Please do not change or delete this link. Read the license! Thanks. :-) -->
Design by <a href="http://www.realitysoftware.ca" title="Website Design">Reality Software</a>

    </div>
    </div>
    <!-- end footer -->

</body>
</html>

Link to comment
Share on other sites

Try this

 

<?php

mysql_connect("localhost", "lee", "leecopter");
mysql_select_db("lee");

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT username FROM users WHERE username='$username'");
$check_num_rows = mysql_num_rows($check);

if ($username=="")
echo "Choose a username";

else if (strlen($username)<=3)
echo "Username is too short";

else 
{
if ($check_num_rows==0)
echo "Username is availible!";

else if ($check_num_rows==1)
echo "Username has already been taken";
}
?>

 

Link to comment
Share on other sites

What about this?

 

<?php

mysql_connect("localhost", "lee", "leecopter");
mysql_select_db("lee");

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT username FROM users WHERE username='$username'");
$check_num_rows = mysql_num_rows($check);

if (strlen($username)<=1)
echo "Choose a username";

else if (strlen($username)<=3)
echo "Username is too short";

else 
{
if ($check_num_rows==0)
echo "Username is availible!";

else if ($check_num_rows==1)
echo "Username has already been taken";
}
?>

 

Link to comment
Share on other sites

The following will (tested) send the field data to your check.php file and display the response that is sent back -

 

$(document).ready(function(){
  $("#username_input").keyup(function(){
    txt=$("#username_input").val();
    $.post("check.php",{username:txt},function(result){
      $("#feedback").html(result);
    });
  });
});

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.