herghost Posted July 23, 2012 Share Posted July 23, 2012 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 23, 2012 Share Posted July 23, 2012 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. Quote Link to comment Share on other sites More sharing options...
herghost Posted July 23, 2012 Author Share Posted July 23, 2012 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.