Hi,
I made a login form on my site but I am new to javascript and do not know how this could be done (See title).
I am trying to make an alert start if one of the login forms are left empty. Any advice is greatly appreciated, thanks in advance!

Ex:

<html>
<head>
<title>Login</title>
<script type="text/javascript">
function show_alert(){
alert("Please make sure all fields are entered!");
}
</script>
</head>
<body>
<?php
echo " <form action='' method='POST'>
       Username: <input type='username' name='username'>
       Password: <input type='password' name='password'>
       <input type='submit' value='Login'>
       </form>";
if ($_POST){
       $username = mysql_escape_string($_POST['username']);
       $password = mysql_escape_string($_POST['password']);
       if ($username&&$password){
              //Standard login script...
       } else {
              //How do I start the javascript if this is true????
       }
}
?>
</body>
</html>

This issue has to do with JavaScript.. Anyway, this is the code:

<script type="text/javascript">
function chk()
{
  if(!formName.username.value || !formName.password.value)
  {
    alert("Please make sure all fields are filled!");
    return false;
  }
}
</script>
<?php
echo '<form name="formName" action="" method="POST" onsubmit="chk();">
  Username: <input type="username" name="username">
  Password: <input type="password" name="password">
<input type="submit" value="Login">
</form>';

Please note that this is not all the code you've written but only the part that needs to be changed.. Pay attention to the new name I've assigned to the form (which is formName). Hope it'll help.

As for this code that you've written:

if ($_POST){
       $username = mysql_escape_string($_POST['username']);
       $password = mysql_escape_string($_POST['password']);
       if ($username&&$password){
              //Standard login script...
       } else {
              //How do I start the javascript if this is true????
       }
}

You don't need to add any JavaScript code in the else statement...
PHP is a server-side language, which means that it's always parsed in the server, then the result is retrieved after the information is sent with the page reloaded (unless you're using AJAX)..

Whereas JavaScript is executed whithin the browser itself without the need to send information to the server and wait for it to be retrieved... So you can cross out the ELSE STATEMENT in the PHP code. (You've started it on lines 22 to 24).

i am student

What's that supposed to mean??

Your code is not a good way to check login page. try with as per Pro2000 suggested.

i am student

Ok... Then...

<script type="text/javascript">
function chk()
{
  if(!formName.username.value || !formName.password.value)
  {
    alert("Please make sure all fields are filled!");
    return false;
  }
}
</script>
<form name="formName" action="" method="POST" onsubmit="return chk();">
  Username: <input type="username" name="username">
  Password: <input type="password" name="password">
<input type="submit" value="Login">
</form>

I've modified the line number 12 where the onsubmit event should be: onsubmit="return chk();"
Also, you needn't and shouldn't open the <?php because you only typed HTML and JS in it..
This way is faster for the server because it won't take time to parse echo and its content.

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.