Jump to content

need help with php and small javascript


Alidad

Recommended Posts

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

Link to comment
Share on other sites

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 ~_~

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.