Chappers Posted December 18, 2006 Share Posted December 18, 2006 Hi,I've made a username/password login system which stores the usernames and passwords inside a MySQL table. What I now want to do is have Javascript check the username a person is trying to create against the usernames already present in the table, so if they try one already taken it will give a javascript alert.I've done this code:[code]<?php$db_hostname = "db.blah.com";$db_name = "me_mysql";$db_username = "me_mysql";$db_pass = "password";$link = mysql_connect ($db_hostname, $db_username, $db_pass);if (!$link){die ('I cannot connect to the database because: ' . mysql_error());}$db_selected = mysql_select_db($db_name,$link);$result = mysql_query("SELECT username FROM members");while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {print_r($row["username"]);}mysql_close($link);?>[/code]This gives me a continuous list of entries in one row with no spaces between each username. How do I create whatever is necessary for Javascript to check (an array?), and how do I get Javascript to check each username which is brought up from the database?If I just get the above code to print one single username from the database, I can then give that username a variable name in PHP, then use <javascript> var userID = "<?php echo $userID; ?>"; </javascript> and then:[code]if ((username.value = userID) && saveit){alert ("The username is taken. Please change it.");username.focus();username.select();saveit = false;}[/code]But I can't work out what to do for listing all field entries under USERNAME and then getting Javascript to check each one against the entry from the form where the user has put their desired username. Bearing in mind that the database will be growing continuously with more and more unique names being added, so more of an array for javascript to check against.Please someone help, I've scoured the net but can't work this out.Thanks,James Link to comment https://forums.phpfreaks.com/topic/31121-converting-mysql-fields-to-array-with-php-for-javascript-checking/ Share on other sites More sharing options...
drifter Posted December 18, 2006 Share Posted December 18, 2006 you are probably not going to do it this way - saving all the usernames would not be good. especially since this array could become very large and every user would need to down load it.you are better off doing this with AJAX and checking on the server if it is already usedthen check again after submit to ensure two people did not create the same username at the same time. Link to comment https://forums.phpfreaks.com/topic/31121-converting-mysql-fields-to-array-with-php-for-javascript-checking/#findComment-143710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.