To start, your javascript isn't being run. You're creating the function, but not calling it, and it doesn't interact with the PHP in any way. You can do that by changing your form tag to this:
<form action="login.php" method="post" onsubmit="return check_empty_field()">
Now the function check_empty_field runs whenever you submit the form. If it returns true, the form gets submitted and the PHP runs. Otherwise, it doesn't. For the function, change it to this (I put in some jQuery because it makes it 10x easier, if you're new to programming you should definitely look into it):
<script Language="JavaScript">
<!--
function check_empty_field() // This is called when the submit
{ // button is pressed
if (form.username.value == "" || form.password.value == "")
{
alert("Please fill in the password or login field.");
return false; // This doesn't submit the form
}
else
{
return true; // This submits the form
}
}
-->
</script>
The jQuery makes a lot of this much easier, which is why I added it in. You can find more about it here. Of course, you'd have to put this at the top of your page, to make the jQuery work:
<script language="javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.min.js"></script>
This gets the jQuery definitions from the Microsoft CDN. It's rather reliable. It would be better to just put jQuery on your site, though, and then put in the path to the file.
Next, the PHP has some issues. Some have been covered by others, and pay attention to the previous answer, but to point out a few issues, session_register
is not used anymore. Now you set session variables with $_SESSION['variablename'] = 'value'
. You also have other issues to worry about, like validation and such. You should Google that.
If you want to know more about any of this, feel free to comment. I also can provide you with sources so you can learn more about the different parts of the code I used.