Javascript Popup in Echo?

I have a register code, and if the username is taken, currently, it takes them to another page to say the username is taken.

Is it possible to do a javascript popup box if the username is taken? here is register.php

<?php
if($_POST["username"] && $_POST["password"] && $_POST["password2"] )
{
$username = $_POST["username"];
$password = $_POST["password"];
$password2 = $_POST["password2"];

    if($password==$password2)
    {
	$password = md5($password);
    $DBservername="localhost";
    $DBusername="******";
    $DBpassword="******";
   $conn =  mysql_connect($DBservername,$DBusername,$DBpassword)or die(mysql_error());
    mysql_select_db("gogreen",$conn);
	
	$dbunames = mysql_query("SELECT * FROM `members` WHERE username='$username'");
	if(mysql_num_rows($dbunames) > 0 ) {
		echo "Username Taken!";	

	} else {
	
    $sql="INSERT INTO `members` (username,password)values('$username','$password')";
    $result=mysql_query($sql,$conn) or die(mysql_error());        
    print "<h1>you have registered sucessfully</h1>";
 
    header("Location: ../index.php");
    }
	}
    else print "Passwords doesnt match";
}



?>

Instead of

echo "Username Taken!";
echo <<<EOF
<script>
alert('Username Taken!')
</script>
EOF;

Above would work, also put exit after your header:


header("Location: ../index.php");
exit();

That actually worked perfect. Now, after it pops up with that, how would I re-direct back to index so they can type in another username? Header wont work for me. ;[
Is this possible?

And if a visitor comes along with javascript turned off in their browser, how will you let them know a username is taken?

They better turn that javascript on! :slight_smile:

You can’t force people to turn javascript on. If you try to, you’re more likely to lose them.