Feel free to move this post if it is in the incorrect category. I was using a validation method for usernames on my website but I would like to make some improvements on it. You typed in your name and it would search and pop-up whether it was available or not. I am looking for a method similar to the one used on this website and many others that checks when the field loses focus. The php code and easiest example I have found is here: http://shawngo.com/wp/blog/gafyd/index.html
$username = $_POST['username']; // get the username
$username = trim(htmlentities($username)); // strip some crap out of it
$file = '/home/js4hire/public_html/gafyd/data.csv'; // Here's the file. Notice the full path.
echo check_username($file,$username); // call the check_username function and echo the results.
function check_username($file_in,$username){
$username=strtolower($username);
$file = file($file_in);
foreach ($file as $line_num => $line) {
$line = explode(',',$line);
$user = trim(str_replace('"','',$line[0]));
if($username == strtolower($user)){
return '<span style="color:#f00">Username Unavailable</span>';
}
}
return '<span style="color:#0c0">Username Available</span>';
}
That example uses a flat file CSV, but I would like to use my MySQL database instead. I have included a snippet from my previous that I believe would tie into this, I'm just not sure how exactly:
require_once dirname(__FILE__).'/../includes/common.inc.php';
require_once dirname(__FILE__).'/../includes/user_functions.inc.php';
$output='';
if (!empty($_POST['user'])) {
$user=sanitize_and_format($_POST['user'],TYPE_STRING,$__field2format[FIELD_TEXTFIELD]);
if (get_userid_by_user($user) || $user=='guest') {
$output=1;
}
}
echo $output;
That common.inc.php calls for the database connection in session.inc.php:
$josh_dblink=db_connect(_DBHOST_,_DBUSER_,_DBPASS_,_DBNAME_);
if (!defined('NO_SESSION')) {
require _BASEPATH_.'/includes/sessions.inc.php';
}
I don't want to have to call the db connection again in that file but I need to get the relevant information and pass it through the php. Any help would be appreciated!