Dynadot

Live registration info checking via Ajax & PHP

Spaceship Spaceship
Watch

OpticPhreak

Code NinjaEstablished Member
Impact
34
So you want to check the user's info before their click submit during registration, but don't wan some heavy JQuery to bloat you down? Well here you go!

See it in action at my project site: http://claimtheday.com/register.php

*do not actually register... I disabled it. But you can try the form.

This will not work as copy and paste. It is a learning example to help you, not do it for you.

This goes into your <head> tags:

Code:
<script type="text/javascript" language="javascript">
  	<!--//
	var xmlHttp;
	function GetXmlHttpObject(){
		var objXMLHttp=null;
		if (window.XMLHttpRequest){
			objXMLHttp=new XMLHttpRequest();
		}
  		if (window.ActiveXObject){
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return objXMLHttp;
	}
	function CheckUsername(u){
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request");
			return;
		}
		var url="ajax/u.validate.php?u="+u;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = function () { 
			if (xmlHttp.readyState == 4) {
				document.getElementById("usernameresult").innerHTML = xmlHttp.responseText;
			}
		};
		xmlHttp.send(null);
	}
    	//-->
</script>



This is how you use it in your form:

Code:
<input autocomplete="off" type="text" class="input-text" name="u" id="u" onkeyup="CheckUsername(this.value);" /> <span id="usernameresult">Status will show here automatically.</span>


Now u.validate.php:

Code:
<?php
/*
        *******************************************************
	PHP coder for hire: LampNinjaMan
	
	Email: [email protected]
	
	Additional skills:
	
	Linux administration, networking, security,
	data entry, cpanel integrations, plesk integrations,
	SEO coding, SEO optimization, JQuery Integration.
        *******************************************************
*/
// open sql connection
require('../config/_config.php');

if (!function_exists('sqlclean')) {
	// clean string to prevent hacking of db
	function sqlclean($data) {
		if (filter_var($data, FILTER_VALIDATE_INT) !== FALSE) {
			return $data;
		} else {
			$data = filter_var($data, FILTER_SANITIZE_STRING);
			return mysql_real_escape_string($data);
		}
	}
}

// make sure the username var is set and not empty before trying to use it.
if (empty($_REQUEST['u'])) {
	// default txt to show when nothing entered
	echo 'Choose a username';exit;
} else {
	// $_REQUEST['u'] isset and not empty... so we continue

	// properly call/define username variable
	$u = strtolower(sqlclean($_REQUEST['u']));

	// check against disallowed usernames
	$bannedUnames = array('legal','support','admin','administrator','webmaster','hitler','satan');
	if (in_array($u,$bannedUnames)) {
   		echo '<span style="color:#FF0000;">Username Taken</span>';exit;
	}

	// is string too long?
      	if (strlen($u) > 45) {
      		echo '<span style="color:#FF0000;">Too Long</span>';exit;
      	}

	// is string too short?
      	if (strlen($u) < 3 ) {
      		echo '<span style="color:#FF0000;">Too Short</span>';exit;
      	}

	// is username in db already?
      	$checkusername = mysql_query('SELECT COUNT(user_id) as Num FROM `users` WHERE `username`="'.$u.'" ') or exit(mysql_error());
      	$result = mysql_result($checkusername, 0, 0);
      	if ($result != 0) {
      		echo '<span style="color:#FF0000;">Username Taken</span>';exit;
      	} else {
      		echo '<span style="color:#347C17;">OK</span>';exit;
      	}
}
exit;
?>

I hope this helps and remember, I am always for hire... no job to big or small... just don't ask me to touch Joomla.... yuk :imho:.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
JQuery

I recommend using JQuery ;) It's always simpler.

Code:
<script type="text/javascript" language="javascript">
  function CheckUsername(u){
    $.get("ajax/u.validate.php?u="+u,function(data){
       $("#usernameresult").html(data);
    })
  }
</script>

And I would actually bind the keyUp event to the input with name='u'. So you have cleaner HTML.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back