Northern Flame Posted December 30, 2007 Share Posted December 30, 2007 I a, creating a registration form for my membership program and I want to check if the username exists before the form is submitted. How can I do this? I attempted to do it with a bit of javascript, but it doesn't seem to be working. Can someone please show me how to do this or modify my script so that it works? this is how Im trying to do it. <script type="text/javascript"> function nameCheck(){ var userName = document.getElementById('username').value; var num = <?php $name = 'userName'; echo mysql_num_rows(mysql_query("SELECT * FROM users WHERE username='$name'")); ?>; if(num == 1){ document.getElementById('alert').innerHTML = "Username " + userName + " Already Exists!"; } </script> <input type="text" name="username" id="username" size="20" onBlur="nameCheck()"> but that doesnt work, the mysql_num_rows returns back 0, anybody have any ideas on how to do this? Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted December 30, 2007 Share Posted December 30, 2007 javascript and php don't work. This is the problem: $name = 'userName'; Php is parsed before javascript will fill in that variable. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted December 30, 2007 Author Share Posted December 30, 2007 Yea I know, but is there a way that I can check if the user exists using PHP and doing it before the user hits "submit"? Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted December 30, 2007 Share Posted December 30, 2007 yeah it's called ajax. You need to do a http request and then use php to check. It's rather difficult and you might as well have them submit it and check. I don't think that this extra work is worth those few seconds to the user. IMO. Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted December 30, 2007 Author Share Posted December 30, 2007 lol alright, I guess Ill just wait for the submit, thanks for the advice! Quote Link to comment 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.