userjosh Posted November 28, 2010 Share Posted November 28, 2010 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! Link to comment https://forums.phpfreaks.com/topic/220049-user-registration-validation/ Share on other sites More sharing options...
dink87522 Posted November 28, 2010 Share Posted November 28, 2010 Don't a lot of sites use AJAX to do that? Link to comment https://forums.phpfreaks.com/topic/220049-user-registration-validation/#findComment-1140549 Share on other sites More sharing options...
userjosh Posted November 28, 2010 Author Share Posted November 28, 2010 There are several ways I could answer that question... But I'll be nice and say, yes. There are a lot of sites that use ajax. Thank you for that observation. Now, anyone that can help with the PHP question I posted? Link to comment https://forums.phpfreaks.com/topic/220049-user-registration-validation/#findComment-1140580 Share on other sites More sharing options...
Rifts Posted November 29, 2010 Share Posted November 29, 2010 you can not do this purely with php. I would recommend php + jquery here is a very simple very easy to follow youtube video on how to do it Link to comment https://forums.phpfreaks.com/topic/220049-user-registration-validation/#findComment-1140990 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.