Im pretty new to coding but when creating some features of my project i wanted to try and be as up to date as possible.
 
So i thought it would be pretty awesome to let the user know when creating a username if it is available before submitting the form, so as im a novice i went and did a bit of research and found this awesome code, of course i needed to change a few things to adapt it for me, but it works quickly, precisely and does exactly what i needed it to do.
 
I found the tutorial from the PHP Academy site, and can be found online at YouTube.
 
When showing my project features to a few students, they thought it was also awesome and asked for the code, so i have included the code below, so they can add it to their projects as needed.
 
1. This code, needs to be placed in the head of the page that you would like the feedback from your database to show. I placed the code in the head of the registration.html page

	<script type="text/javascript">
	$(document).ready(function() {
		$('#usernameFeedback').load('scripts/check.php').show();
		
		$('#username_input').keyup(function() {
		  $.post('scripts/check.php', { username: form.username.value },
		  function(result) {
		 	 $('#usernameFeedback').html(result).show();
		  
			});	
		});		
		
	});
	</script>

 
2. Include this code in the same page and add in where your would like the feedback to be displayed, for example i have added it beside the username input field.
 

<div id="usernameFeedback"> 
<p id="usernameAvailable"></p>
</div>

 
3. This is where the jQuery in the head of the registration page connects to, i am calling it check.php and i have put it in my scripts folder. Also i have used mysqli, so you may need to adapt this and take out certain connections.
 

<?php

$conn = mysqli_connect('localhost', 'your_username', 'your_password');
mysqli_select_db($conn, "your_database");

$username = mysqli_real_escape_string($conn, $_POST['username']);
												
$check = mysqli_query($conn, "SELECT username FROM usertrail WHERE username='$username' ");
$check_num_rows = mysqli_num_rows($check);

if($username==NULL)
	echo "Choose a username";
  else if (strlen ($username) <=8)
  	echo "Too Short";
  else
  {
  	if ($check_num_rows==0)
  		echo "This awesome username is available";
  	else if($check_num_rows==1)
  		echo "This awesome username is taken";
  }

?>
© Copyright of Alexandra Milne 2013 | Privacy Policy | Disclaimer | Disclosure