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, [email protected], testuser, sha1pass

 

 

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

 

 

 

 

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>

Archived

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

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