After following replies to a previous question on here, I used jQuery to return whether a username was available or not, I found some code on a site, which showed me how to do it. From what I can see, the code seems to be structured correctly, and is connecting to the Database, however even if the name isn't in the Database it returns that it is not Available.
Here is the jQuery(And Registration form) which is included in the register.php file (Will link below jQuery).
<div class="registration">
<h3>Register - Create a new Account</h3>
<form action="confirm-registration.php" method="POST">
<input type="text" name="create-username" id="username" placeholder="Username" maxlength="25" /><input type='button' id='check_username_availability' value='Check Availability'>
<div id='username_availability_result'></div> <br />
<input type="password" name="create-password" placeholder="Password" /> <br />
<input type="password" name="confirm-password" placeholder="Confirm Password" /> <br />
<input type="email" name="create-email" placeholder="E-mail Address" maxlength="45" /> <br />
<input type="submit" value="Complete Registration" />
</form>
<form>
<input type="submit" value="Cancel" />
</form>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCJnj2nWoM86eU8Bq2G4lSNz3udIkZT4YY&sensor=false"></script>
<script type="text/javascript" src="/check-user.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//the min chars for username
var min_chars = 3;
//result texts
var characters_error = 'Minimum amount of chars is 3';
var checking_html = 'Checking...';
//when button is clicked
$('#check_username_availability').click(function(){
//run the character number check
if($('#username').val().length < min_chars){
//if it's bellow the minimum show characters_error text '
$('#username_availability_result').html(characters_error);
}else{
//else show the cheking_text and run the function to check
$('#username_availability_result').html(checking_html);
check_availability();
}
});
});
//function to check username availability
function check_availability(){
//get the username
var username = $('#username').val();
//use ajax to run the check
$.post("check_username.php", { username: username },
function(result){
//if the result is 1
if(result > 0){
//show that the username is available
$('#username_availability_result').html(username + ' is Available');
}else{
//show that the username is NOT available
$('#username_availability_result').html(username + ' is not Available');
}
});
}
</script>
The code below links in the final file relevant to this - check-username.php. This is the file that queries the database with the username and then returns it. I will link that below this php.
<?php
include '../connect.php';
if (isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true ) {
header('Location : http://localhost/blog');
} else {
echo include 'registration_form.php';
require 'check_username.php';
}
?>
<?php
$username = mysql_real_escape_string(isset($_POST['create-username']));
//mysql query to select field username if it's equal to the username that we check '
$result = mysql_query('SELECT user_name from users where user_name = "'. $username .'"');
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysql_num_rows($result) > 0){
//and we send 0 to the ajax request
echo 0;
} else {
//else if it's not bigger then 0, then it's available '
//and we send 1 to the ajax request
echo 1;
}
Any help with this would be appreciated, this isn't my code and I've not done much jQuery, so after trying a few things I'm now at a complete loss on how to fix it.
Thanks, Ben