0

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>
4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Pro2000
1

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.

Edited by Pro2000: n/a

0

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).

Edited by Pro2000: n/a

0
<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.

Edited by Pro2000: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.