Jump to content

[SOLVED] error handling


ngreenwood6

Recommended Posts

I am in need of some help with error handling. I have my main page:

 

<?php
include("includes/variables.php");
?>
<html>
<head>
<link href="includes/style.css" rel="stylesheet" type="text/css" />
<title><?php echo $sitename; ?></title>
</head>

<body>

<center>

<h1>Login Here!</h1>

<form name="login_form" method="post" action="check_user.php">
<table id="login_table">
<tr>
<td>Username</td>
<td>:</td>
<td><input name="username" type="text"></td>
<td id="username_empty"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password"></td>
<td id="password_empty"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="submit_login" value="Log In"></td>
</tr>
</table>
</form>

</center>

</body>
</html>

 

Then I have the check_user.php:

<?php
//include the variables
include("includes/variables.php");

//define the variables from the form
$username = strtolower($_POST['username']);
$password = $_POST['password'];

//connect to the database
$connect = mysql_connect($host, $db_user, $db_pass) or die("could not connect");

//select the database
$select_db = mysql_select_db($db) or die("could not select database");

//query for the database
$query = "SELECT * FROM $table WHERE username='$username'";

//get the results
$results = mysql_query($query);

//number the rows
$num_rows = mysql_num_rows($results);

//put it into an array
$row = mysql_fetch_array($results);

//give the username and password from the database a variable
$row_username = $row['username'];
$row_password = $row['password'];

//if the username is blank
if($username == "")
{
echo "Please enter a username!";
}
//if the password is blank
else if($password == "")
{
echo "Please enter a password!";
}
//if the username is not in the database
else if($num_rows < 1)
{
echo "That username is not in the database!";
}
//if the password is not correct
else if ($password != $row_password)
{
echo "The password you entered does not match!";
}
else
{
echo "You are good!";
}


?>

 

At this point it checks that the username and password are not empty and that they are in the database and match the database. If everything is good it says "You are good!". When the user enters something incorrect it displays the error on a separate page. I want it to display the error on the same page without submitting the form. I know you can do it with ajax but am lost. Can someone please help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.