pinacoladaxb Posted September 21, 2008 Share Posted September 21, 2008 How would I go about including a username availability check into my registration form validation javascript script for my site? I posted this in "PHP Help" because I know this will have to do with sql querys. Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/ Share on other sites More sharing options...
Adam Posted September 21, 2008 Share Posted September 21, 2008 Use something like: <?php $username = mysql_real_escape_string($_POST['username']); $check = mysql_query("SELECT userField FROM yourUserTable WHERE userField = '{$username}'"); if (mysql_num_rows($check) > 0) { print 'Username taken!'; } else { print 'Username available!'; } ?> Adam Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/#findComment-647303 Share on other sites More sharing options...
twilitegxa Posted September 21, 2008 Share Posted September 21, 2008 I have a register.php page that checks the username against the database and returns a value of Your username is already in use. Please select another if already in use. This is my php, in case it helps: <?php session_start(); // Connect to MySQL database: $access = mysql_connect('localhost','root','') or die ('Could not connect to database'); mysql_select_db('smrpg',$access) or die ('Could not select table'); # # $error = array(); if(isset($_POST['username'])) { $result = @mysql_query('SELECT username FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\''); if($row = @mysql_fetch_row($result)) { array_push($error, 'Your username is already being used. Please select another.'); } $len = strlen($_POST['username']); if($len < 3 || ($len > 20)) { array_push($error, 'Your username must be between 3 and 20 characters long.'); } $len = strlen($_POST['password']); if($len < 6 || ($len > 20)) { array_push($error, 'Your password must be between 6 and 20 characters long.'); } $len = strlen($_POST['name']); if($len > 100) { array_push($error, 'Sorry, your name can be no longer than 100 characters long.'); } if(!$_POST['name']) { array_push($error, 'You must provide your name'); } if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $_POST['email']) == false) { array_push($error, 'Your e-mail address is incorrect'); } $len = strlen($_POST['email']); if($len > 255) { array_push($error, 'Sorry, your e-mail address is too long.'); } if(!$error) { @mysql_query('INSERT INTO `users` (username, password, name, email) VALUES (\''.mysql_real_escape_string($_POST['username']).'\', \''.mysql_real_escape_string(md5($_POST['password'])).'\', \''.mysql_real_escape_string($_POST['name']).'\', \''.mysql_real_escape_string($_POST['email']).'\')'); header('Location: login.php'); die('<a href="login.php">Login</a>'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/#findComment-647306 Share on other sites More sharing options...
FIREBALL5 Posted September 21, 2008 Share Posted September 21, 2008 Guys, he said he wants a javascript script, not a php script. But if you want a javascript, I don't think that is going to happen. I hear AJAX is good for that kind of stuff, but I don't know it personally. Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/#findComment-647314 Share on other sites More sharing options...
Minase Posted September 22, 2008 Share Posted September 22, 2008 he can do what he want with javascript only .... but it isnt soo good you can retrive all usernames with PHP and use them into array in JS after that check the inserted username with arrays this works but its not recomended at all.if the list is huge than it can take a while.. so i dont think its a good ideea Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/#findComment-647323 Share on other sites More sharing options...
Adam Posted September 22, 2008 Share Posted September 22, 2008 oh aye.. must have skimmed over it ??? Well as fireball5 said, AJAX is the answer.. but the bit of code I showed you before with a few small changes will function as the PHP side of it... <?php // connect $username = mysql_real_escape_string($_POST['username']); $check = mysql_query("SELECT userField FROM yourUserTable WHERE userField = '{$username}'"); if (mysql_num_rows($check) > 0) { print 1; } else { print 0; } ?> Then just follow a tutorial such as http://www.w3schools.com/Ajax/ajax_database.asp .. to setup the javascript side of it. Any help? Adam Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/#findComment-647327 Share on other sites More sharing options...
twilitegxa Posted September 22, 2008 Share Posted September 22, 2008 I've heard it better, if you're even going to use JavaScript to validate, to still use PHP to validate on the server end because JavaScript only validates on the user end and they can disable it as well. Covering both ends seems to work better than just the JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/125224-form-validation-available-username-checker/#findComment-647353 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.