View Full Version : JS stop loading
Ok I have a form, and im learning Javascript...so I applied them together! Anyways, it checks if you entered anything into the username field, and if not, displays an alert saying to put in a name, but it still goes on and loads it (and comes up on login.php with 'Please enter a username'. If they dont enter a name, I want the function to alert and then not go any further. This is the code:
<script type="text/javascript">
<!--
function checkInput() {
var user = document.loginform.user.value;
var pass = document.loginform.pass.value;
if (user.length == 0 && pass.length == 0) {
alert("You must enter a name and password in order to login!");
}
}
// -->
</script>
birdbrain
11-11-2005, 06:00 AM
Hi paim,
the normal way to call a function would be something like this..
<form action="login.php" mathod ="post" onsubmit="return checkInput()">
...I would need to see you form, though, to check your script.
Yes yes, thats what I have in the form at the moment.
<?php
if (!$_SESSION['user']) { ?>
<table width="90%" border="0">
<tr><td colspan="2"><center>Login:</center></td></tr>
<form action="login.php" method="post" onsubmit="return checkInput()" name="loginform">
<tr><td width="30%">
User:</td><td width="70%"><input type="text" name="user" size="15"></td></tr>
<tr><td width="30%">Pass:</td><td width="70%"><input type="password" name="pass" size="15"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="submit" value="Login"></center>
</form></td></tr></table>
<a href="register.php">Register for a new account!</a>
<?php }
Thats the login form. Login.php:
<?php
include('connection.php');
$user = $_POST['user'];
$pass = $_POST['pass'];
$check = "SELECT ID, Name, pass, userGroup, Email, IP FROM user WHERE Name = '$user'";
$select = mysql_query($check);
if ($select)
{
$row = mysql_fetch_assoc($select);
$_SESSION['rank'] = $row['userGroup'];
$_SESSION['id'] = $row['ID'];
$_SESSION['email'] = $row['Email'];
$_SESSION['IP'] = $row['IP'];
if ($pass == '') {
echo "Please enter a password!";
}
elseif ($row['pass'] != $pass) {
echo "Incorrect Password!<br />";
echo '<a href="index.php">Back</a>';
}
elseif ($row['pass'] == $pass)
{
echo "You are logged in $user!<br />";
echo '<a href="user_cp.php">Continue</a>';
$_SESSION['user'] = "$user";
}
}
else
{
echo "Query failed!";
}
?>
I want the function to NOT send teh user to the login.php screen if there is no username, but it just goes to login.php and comes up with 'Please enter a password'.
birdbrain
11-11-2005, 07:08 AM
Hi paim,
try it like this...
<script type="text/javascript">
<!--
function checkInput() {
var user = document.loginform.user.value;
var pass = document.loginform.pass.value;
if((user=="")||(pass== "")) {
alert("You must enter a name and password in order to login!");
return false;
}
}
// -->
</script>
Yes! it works! Thanks birdbrain. It stopped loading everything once the alert came up :)
birdbrain
11-11-2005, 07:19 AM
No problem, you're welcome. :)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.