Alidad Posted June 8, 2007 Share Posted June 8, 2007 Hi, i'm trying to solve problme with check user name sign up to make sure that user name is not taken. what i want is to have popup message in javascripts to say "the user name is taken, please use other user name". What I wrote is wrote a function for popup message in javascript which please see code: <SCRIPT LANGUAGE="JavaScript"> function UserNameCheck() window.alret ('user name is taken, please selet other user name'); </script> And then I wrote php script to check database to see if that user name are avaliable or not. please see that script <?php mysql_connect("localhost", "username", "password"); mysql_select_db("DatabaseName"); function check_user_exist($username) { $username = mysql_real_escape_string($username); $query = mysql_query("SELECT * FROM member WHERE username = '{$username}'") or die(mysql_error()); $rows = mysql_num_rows($query); if($rows > 0) { return true; } else { return false; } } if(check_user_exist($_POST["userName"])) { UserNameCheck(); } } ?> <head> <title>check user name</title> </head> <body> <form id="form1" name="form1" method="post" action=""> User Name: <label> <input type="text" name="userName" id="userName" /> </label> <label> <input type="submit" name="button" id="button" value="Submit" /> </label> </form> </body> </html> but i'm getting error message said that i'm missing one "}" in (RED) Area. i'm not sure what it is, I really need your help please how can i solve this. AM Quote Link to comment https://forums.phpfreaks.com/topic/54798-need-help-with-php-and-small-javascript/ Share on other sites More sharing options...
Guest Posted June 8, 2007 Share Posted June 8, 2007 Oh, I see what you're trying to do. You're trying to invoke UserNameCheck() in the javascript FROM PHP. Unfortunately, you cannot do that. PHP runs on the server, and Javascript runs on the client side. By the time the Javascript so much as appears, the PHP is done executing. So PHP and javascript cannot really communicate (in realtime, at least). However, there is an alternative: You may want to look into AJAX w/PHP for something like this. AJAX is a technique used in javascript to run a seperate file (a PHP script, per say), that will output a result (like "Username unavailable" or "Username available"), and immediately take whatever is printed to screen in that script and place it into the current page (without the need to reload the entire page). It's a bit too much to cover in one post though ~_~ Quote Link to comment https://forums.phpfreaks.com/topic/54798-need-help-with-php-and-small-javascript/#findComment-270981 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 if(check_user_exist($_POST["userName"])) { echo "<script type=\"text/javascript\">alert('user name is taken, please selet other user name');</script>"; } Not sure if that'll work... depends where in the page you're running your checks. Quote Link to comment https://forums.phpfreaks.com/topic/54798-need-help-with-php-and-small-javascript/#findComment-270982 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Share Posted June 8, 2007 8ball is right, although I think you've got an extra bracket. if(check_user_exist($_POST["userName"])) { UserNameCheck(); } }// this one shouldn't be there ?> Quote Link to comment https://forums.phpfreaks.com/topic/54798-need-help-with-php-and-small-javascript/#findComment-270983 Share on other sites More sharing options...
Guest Posted June 8, 2007 Share Posted June 8, 2007 Another way you can go about doing it is you can do: <?php mysql_connect("localhost", "username", "password"); mysql_select_db("DatabaseName"); function check_user_exist($username) { $username = mysql_real_escape_string($username); $query = mysql_query("SELECT * FROM member WHERE username = '{$username}'") or die(mysql_error()); $rows = mysql_num_rows($query); if($rows > 0) { return true; } else { return false; } } if(check_user_exist($_POST["userName"])) { $msg = 'User already exists'; } ?> <head> <title>check user name</title> </head> <body> <p><?php echo $msg?></p> <form id="form1" name="form1" method="post" action=""> User Name: <label> <input type="text" name="userName" id="userName" /> </label> <label> <input type="submit" name="button" id="button" value="Submit" /> </label> </form> </body> </html> This is the old tried and true method: if the username doesn't exist, then $msg is empty and won't appear. If it does exist, it will appear with a message like "The username is unavailable". This way you can avoid javascript altogether. (Of course this is a bit of a crude example, but it works) Quote Link to comment https://forums.phpfreaks.com/topic/54798-need-help-with-php-and-small-javascript/#findComment-270986 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.