I am trying to display an alert box using javascript in my login php file.

When a user logged in using a wrong username or password, then a messagebox will display.

This is what i have so far:

$query="SELECT * FROM info WHERE username ='$user' && password ='$pass' ";
		  $res=mysql_query($query);
		  if (mysql_num_rows($res) == 0) {
		           echo"<script language="javascript">window.alert('Invalid password.')</script>";
				   echo "<br>";
		     }
		   else {
				   echo "Access Granted";
				   echo "<br>";
		         }

Can't figure it out.

Recommended Answers

All 3 Replies

You should escape the double quotes in line 4:

echo"<script language=\"javascript\">window.alert('Invalid password.')...

or u can use this
: echo("<script>alert('Invalid password.')</script>");

The answer has already been posted, but for the sake of easiness I'm giving you the complete code:

if (mysql_num_rows($res) == 0) {
echo"<script language='javascript'>window.alert('Invalid password.')</script>";
echo "<br>";
}
else {
echo "Access Granted";
echo "<br>";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.