<?php
// connection to MySQL server
mysql_connect('localhost','root','');
mysql_select_db('administration');

if (isset($_POST['submit'])) {
  $loginUsername=$_POST['username'];
  $loginPassword=$_POST['password']; 
  $MM_redirectLoginSuccess = "validated.php";
  $MM_redirectLoginFailed = "admin.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db('administration');
  
	 if(empty($_POST['username']) && empty($_POST['password'])) {
  	 die('<div class="error_up">Both field was blank</div>
		<br><br><div class="footer_padding">
			&copy; Copyright Cycle Tracks <span>®</span></div>
			');
	 }
	 if(empty($_POST['username'])) {
  	 die('<div class="error_up">Username field was blank</div>
		<br><br><div class="footer_padding">
			&copy; Copyright Cycle Tracks <span>®</span></div>
			');
	 }
	 if(empty($_POST['password'])) {
  	 die('<div class="error_up">Password field was blank</div>
		<br><br><div class="footer_padding">
			&copy; Copyright Cycle Tracks <span>®</span></div>
			');
	 }
	
  $loginUsername = get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername);
  $password = get_magic_quotes_gpc() ? $password : addslashes($password);
  $LoginRS_query = "SELECT username, password FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  
  if ($loginFoundUser) {
    
    echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
	}
  else {
  
    echo "<script type='text/javascript'>location.href='$MM_redirectLoginFailed';</script>";
}
}
?>
<html>
<head>
	<title>Cycle Tracks Portal - Validation Page</title>
		<style type="text/css" media="all">@import "images/style.css";
		</style>
	<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="rss/"/>		
</head>
<body>
</body>
</html>

HELP needed on:
1. As you can see above, there is a validation for empty username and password.
2. At the bottom of the PHP codes, I've included the HTML codes.
3. I want the validation error messages to get printed into the body tag of the HTML codes without moving the HTML codes up before the PHP codes.
4. Please help.

don't use the die statement. the html is not being outputted because the nothing is executed or echoed the page after the die statement has been used. put the error message into a variable and echo it inside the html.

How to I echo the "errors" according to the the errors in the html. Assume that I've assigned the error checking validation to the respected variables.

just echo it. die is an alias of exit and once exit/die is encountered, the execution of the script stops there itself. You can print the error in this fashion !

<?php
if(isset($_POST['submit'])){
  if(empty($_POST['name'])){
    $error="Name field is empty !";
  } else {
  //process the information 
 }
}
?>
<html>
<body>
<form method="post" action="test.php">
<input type="text" name="name"><?php echo $error; ?><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

So, If the textbox is empty, then $error will have the value, Name field is empty.. If it isn't empty, $error will be null. If its empty, the script wont do anything but show the error message next to the textbox.

Thanks Nav33n. solved.

:) You are welcome!

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.