Jump to content

My 1st attempt at an ajax script - check username - not working!


herghost

Recommended Posts

Hi Guys

 

This is my first attempt at an ajax script, so please be gentle!

 

Basically my script is meant to check a db to see if the username exists, however it just hangs on the loading.gif

 

 

<script type="text/javascript">
$(document).ready(function()
{
$("#username").change(function() 
	{ 
		var username = $("#username").val();
		var msgbox = $("#status");
		if(username.length < 6)
			{
				$("#status").html('<img src="img/cancel.png" />  Username must be at least 6 characters');
				$('#username').css('background-image', 'url("../forms/img/cancel.png")');
				document.getElementById("registerNew").disabled=true;
			}
		if(username.length > 5)
			{

				$("#status").html('<img src="img/loading.gif" /> ...Checking Username');
				$('#username').css('background-image', 'url("../forms/img/asterisk_orange.png")');
				document.getElementById("registerNew").disabled=true;
				jQuery.ajax({
   								type: "POST",
   								url: "usernamechecker.php",
   								data: 'username='+ username,
   								cache: false,
   								
							success: function(response)
								{
									if(response = 0)
										{
										 $('#username').css('background-image', 'url("../forms/img/tick.png")');
										 document.getElementById("registerNew").disabled=false;
										}
									else{
										$('#username').css('background-image', 'url("../forms/img/cancel.png") Username already in use');	
										}

								}
							});
			}
	});

});

</script>

 

usernamechecker.php

<?php
include('../class/class.dbconnect.inc.php');

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

$db = new dbconnect();
$db->connect();
$db->select('tbl_users','username','username="'.$username.'"');
$amount = count($db->getResult());
echo $amount;
?>

 

If I set a variable username in the checker manually and call the page I get the correct response from the database.

 

My table (tbl_users)

 

id, email, username, password

1, test@test.com, testuser, sha1pass

 

 

Can anyone point me in the direction of what I am doing wrong?

 

 

 

 

Link to comment
Share on other sites

usernamechecker.php - is this file in the same directory as the one in your URL?

You should probably use the full URL just to be safe.

 

Install Firebug and you can easily debug this stuff.

 

Hi Jesirose

 

Many thanks for your response, firebug is fantastic!

 

fixed script - I was forgetting to update my html content for the span div

 


<script type="text/javascript">
$(document).ready(function()
{
$("#username").change(function() 
	{ 
		var username = $("#username").val();
		var msgbox = $("#status");
		if(username.length < 6)
			{
				$("#status").html('<img src="img/cancel.png" />  Username must be at least 6 characters');
				$('#username').css('background-image', 'url("../forms/img/cancel.png")');
				document.getElementById("registerNew").disabled=true;
			}
		if(username.length > 5)
			{

				$("#status").html('<img src="img/loading.gif" /> ...Checking Username');
				$('#username').css('background-image', 'url("../forms/img/asterisk_orange.png")');
				document.getElementById("registerNew").disabled=true;
				jQuery.ajax({
   								type: "POST",
   								url: "http://localhost/logtrav/forms/usernamechecker.php",
   								data: 'username='+ username,
   								cache: false,
   								
							success: function(response)
								{
									if(response == 0)
										{
										 $('#username').css('background-image', 'url("../forms/img/tick.png")');
										 document.getElementById("registerNew").disabled=false;
										 $("#status").html('');
										}
									else{
										$('#username').css('background-image', 'url("../forms/img/cancel.png")');
										$("#status").html('Username already in use');
										}

								}
							});
			}
	});

});

</script>

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.