Cant figure out how to show do form validation work

Hey guys, I am trying to add form validation to my form like if the fields were filled out or not, I got it to work for the first form, to show the error on THE SAME PAGE, thats the whole thing I need, for it to show the error on the same page.

Here is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Website Installation</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="installer-styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="container">
        <div id="header">
            <a href="installer.php">Website Installation</a>
        </div>
        <div id="main">
            <div class="pad25">
                <?php
                
                if( isset( $_POST['submit'] ) ) {
                    if( $_POST['nextstep'] == "checkuser" ) { //start function for the checkuser stage
                        if( empty( $_POST['db_name'] ) || empty( $_POST['db_user'] ) || empty( $_POST['db_pass'] ) || empty( $_POST['db_host'] ) ) { 
                            $error = "form not fully filled, reshow first form";
                        }else{
            
                            $form = '
 <FORM method="post" action="' . $_SERVER['PHP_SELF'] . '">  
                            second form<br>
                               <input type="text" name="user3"><br>
                               <input type="text" name="user4"><br>
                               <input type="hidden" name="nextstep" value="checkanswer">
                               <input type="submit" name="submit" value="Submit Form"><br>
                            </FORM>';
                            
                        } //end function for the checkuser stage
                    }elseif ($_POST['nextstep']=="checkanswer") { //start function for the checkanswer stage
                        if(empty($_POST['user3']) || empty($_POST['user4']) ) {
                            $error = "form not fully filled, reshow first second";
                        }else{
                    
                        } //start function for the checkuser stage 
                    }
                    if(isset($error)){
                        echo "<p class=\"error\">" . $error . "</a>";    
                    }
                }
                
                if( isset($form) ) {
                    echo $form;
                }else{
                    
                ?>
                <h1>Step 1: Database Information</h1><br />
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form">
                    <p>Enter your database connection settings. These settings will be inserted into <code>inc/config.inc.php</code> and will be used by the application.</p>
                    <table style="width: 100%;">
                        <tr>
                            <th class="col1">Database Name</th>
                            <td class="col2"><input name="db_name" type="text" size="20" /></td>
                            <td class="col3">The name of the database to use.</td>
                        </tr>
                        <tr>
                            <th class="col1">Username</th>
                            <td class="col2"><input name="db_user" type="text" size="20" /></td>
                            <td class="col3">Your MySQL username.</td>
                        </tr>
                        <tr>
                            <th class="col1">Password</th> 
                            <td class="col2"><input name="db_pass" type="password" size="20" /></td>
                            <td class="col3">Your MySQL password.</td>
                            </tr>
                        <tr>
                            <th class="col1">Database Host</th>
                            <td class="col2"><input name="db_host" type="text" size="20" value="localhost" /></td>
                            <td class="col3">Most likely won't need to change this value.</td>
                        </tr>
                    </table><br />
                    <input type="hidden" name="nextstep" value="checkuser">
                    <input type="submit" name="submit" class="button" style="float:right;" value="Submit Form ›"><br>
                </form>
                <?php
                
                }
                ?>
            </div>
        </div>
        <br class="clr" />
    </div>
    <br class="clr" />
    <div id="footer">
        <div class="footer_inner">&copy; 2010 | Bucket</div>
    </div>
</body>
</html>

I made the error show up for the first form, but cant get it to work on the second form, when the second form gets filled out not all the way, it shows the error on the first for, I am trying to make it show the error on the second.

Can someone help me out please?

This is what I had before, it worked fine, but I hated that the errors would be shown on another page after submit.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Website Installation</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link href="installer-styles.css" rel="stylesheet" type="text/css" />
 </head>
 <body>
  <div id="container">

  <div id="header">
   <a href="installer.php">
   Website Installation</a>
   </div>
    <div id="main">
    <div class="pad25">



<?php
if(isset($_POST['submit'])) {

if ($_POST['nextstep']=="checkuser") { //start function for the checkuser stage
if(empty($_POST['db_name']) || empty($_POST['db_user']) || empty($_POST['db_pass']) || empty($_POST['db_host']) ) { 

echo "form not fully filled, reshow first form";
}
else {
?>

<HTML>
<HEAD><title>Using PHP_SELF</title></HEAD>
<BODY>
<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
second form<br>
   <input type="text" name="user3"><br>
   <input type="text" name="user4"><br>
   <input type="hidden" name="nextstep" value="checkanswer">
   <input type="submit" name="submit" value="Submit Form"><br>
</FORM>
</BODY>
</HTML>

<?php
} //end function for the checkuser stage
?>

<?php
 }         
if ($_POST['nextstep']=="checkanswer") { //start function for the checkanswer stage
if(empty($_POST['user3']) || empty($_POST['user4']) ) {
 
echo "form not fully filled, reshow first second";
}
else {
?>

complete

<?php
} //start function for the checkuser stage
?>

 
<?php
} 
}
else {
?>
 



<h1>Step 1: Database Information</h1><br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form">

  <p>Enter your database connection settings. These settings will be inserted into <code>inc/config.inc.php</code> and will be used by the application.</p>
  <table style="width: 100%;">
    <tr>
      <th class="col1">Database Name</th>
      <td class="col2"><input name="db_name" type="text" size="20" /></td>
      <td class="col3">The name of the database to use.</td>

    </tr>
    <tr>
      <th class="col1">Username</th>
      <td class="col2"><input name="db_user" type="text" size="20" /></td>
      <td class="col3">Your MySQL username.</td>
    </tr>
    <tr>
      <th class="col1">Password</th>

      <td class="col2"><input name="db_pass" type="password" size="20" /></td>
      <td class="col3">Your MySQL password.</td>
    </tr>
    <tr>
      <th class="col1">Database Host</th>
      <td class="col2"><input name="db_host" type="text" size="20" value="localhost" /></td>
      <td class="col3">Most likely won't need to change this value.</td>
    </tr>
  </table><br />
   <input type="hidden" name="nextstep" value="checkuser">
   <input type="submit" name="submit" class="button" style="float:right;" value="Submit Form ›"><br>
</form>






<?php
}
?>

     </div>
    </div>
 <br class="clr" />
  </div>

 <br class="clr" />
   <div id="footer">
    <div class="footer_inner">
&copy; 2010 | Bucket
    </div>
   </div>

 </body>
</html>

Thank You.