I am trying to make a simple web based form that inserts customer data into a mysql database. I purchased captcha code to add to an existing form that works without the captcha code.

The form works fine if the user enters all fields correctly. If the user enters the wrong captcha code it displays the error message on a new page and requests the user to click the back button and try again however, all the data that the user has entered is gone which is not very user friendly. I would like the captcha error to appear the same as the other error messages I have created. How do I do this?

Here is my form code:
PHP Code:
<? php
if (isset($_POST['submit']))
{
    $Turing_code = $_REQUEST["Turing"]; 
    if ( CheckCaptcha($Turing_code) !=1 )
    {
echo "<p><b><font color=red>The Image Verification Code you entered is invalid. Please press the Back button of your browser and try again</font></b></p><br>";
    return 1;
    }
// Initialize the variables.
$message = NULL;

//check for empty fields
$fname = $_POST['fname'];
$fnamestatus = true;
if (strlen($_POST['fname'])==0)
{
    $fnamestatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your First Name</span></p>';
    }
    
$lname = $_POST['lname'];
$lnamestatus = true;
if (strlen($_POST['lname'])==0)
{
    $lnamestatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your Last Name</span></p>';
    }
    
$email = $_POST['email'];
$emailstatus = true;
if (strlen($_POST['email'])==0)
{
    $emailstatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your Email Address</span></p>';
    }
    $email = $_POST['email'];
    
$phone = $_POST['phone'];
$phonestatus = true;
if (strlen($_POST['phone'])==0)
{
    $phonestatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your Telephone Number</span></p>';
    }
    $phone = $_POST['phone'];
    
$address = $_POST['address'];
$addressstatus = true;
if (strlen($_POST['address'])==0)
{
    $addressstatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your Street Address</span></p>';
    }
    
$city = $_POST['city'];
$citystatus = true;
if (strlen($_POST['city'])==0)
{
    $citystatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your City</span></p>';
    }

$province = $_POST['province'];
$provincestatus = true;
if (strlen($_POST['province'])==0)
{
    $provincestatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your Province</span></p>';
    }

$postalcode = $_POST['postalcode'];
$postalcodestatus = true;
if (strlen($_POST['postalcode'])==0)
{
    $postalcodestatus = FALSE;
    $message .= '<p><span style=\'color: red\'>Please enter your Postal Code</span></p>';
    }
// check for duplicate record 
$duplicates = TRUE;
$checkrec = "SELECT COUNT(*) FROM tkt_req WHERE email = '$email'"; 
$resultck = mysql_query($checkrec); 
$num = mysql_fetch_row($resultck); 
if ($num[0] > 0)
    { 
$duplicates = FALSE;
$message .='<p class=\'whitebkgrnd\'><span style=\'color: red\'>Error: Duplicate request found<br>This Email address has already been entered into the Priority Ticket Lottery</span><br></p>';
    }
// If nothing went wrong proceed to insert record
if ($fnamestatus && $lnamestatus && $emailstatus && $phonestatus && $addressstatus && $citystatus && $provincestatus && $postalcodestatus && $duplicates) { 

//insert statement list table fields
$fname = trim($fname);
$lname = trim($lname);
$email = trim($email);
$coname = trim($coname);
$phone = trim($phone);
$phone2 = trim($phone2);
$address = trim($address);
$address2 = trim($address2);
$city = trim($city);
$province = trim($province);
$postalcode = trim($postalcode);
$comments = trim($comments);

$fname = htmlspecialchars($fname);
$lname = htmlspecialchars($lname);
$email = htmlspecialchars($email);
$coname = htmlspecialchars($coname);
$phone = htmlspecialchars($phone);
$phone2 = htmlspecialchars($phone2);
$address = htmlspecialchars($address);
$address2 = htmlspecialchars($address2);
$city = htmlspecialchars($city);
$province = htmlspecialchars($province);
$postalcode = htmlspecialchars($postalcode);
$comments = htmlspecialchars($comments);

$sql = "INSERT INTO tkt_req (fname, lname, email, coname, phone, phone2, address, address2, city, postalcode, province, comments) VALUES ('$fname', '$lname', '$email', '$coname', '$phone', '$phone2', '$address', '$address2', '$city', '$postalcode', '$province', '$comments')";
$result = mysql_query($sql, $db);

//select statement
$fname = stripslashes($fname);
$lname = stripslashes($lname);
$email = stripslashes($email);
$coname = stripslashes($coname);
$phone = stripslashes($phone);
$phone2 = stripslashes($phone2);
$address = stripslashes($address);
$address2 = stripslashes($address2);
$city = stripslashes($city);
$province = stripslashes($province);
$postalcode = stripslashes($postalcode);
$comments = stripslashes($comments);
//display info into table
echo "
<p>You have submitted the following information :</p>

<table border='0' cellspacing='0' cellpadding='1'>
  <tr>
    <td width=\"25%\"><p><strong>Name:</strong></p></td>
    <td width=\"75%\"><p>$fname $lname</p></td>
  </tr> 
  <tr>
    <td width=\"25%\"><p><strong>Email:</strong></p></td>
    <td width=\"75%\"><p>$email</p></td>
  </tr> <tr>
    <td width=\"25%\"><p><strong>Company:</strong></p></td>
    <td width=\"75%\"><p>$coname</p></td>
  </tr> <tr>
    <td width=\"25%\"><p><strong>Telephone:</strong></p></td>
    <td width=\"75%\"><p>$phone <br>$phone2</p></td>
  </tr> <tr>
    <td width=\"25%\"><p><strong>Address:</strong></p></td>
    <td width=\"75%\"><p>$address<br>$address2</p></td>
  </tr> <tr>
    <td width=\"25%\"><p><strong>City:</strong></p></td>
    <td width=\"75%\"><p>$city</p></td>
  </tr> <tr>
    <td width=\"25%\"><p><strong>Province:</strong></p></td>
    <td width=\"75%\"><p>$province</p></td>
  </tr> <tr>
    <td width=\"25%\"><p><strong>Postal Code:</strong></p></td>
    <td width=\"75%\"><p>$postalcode</p></td>
  </tr> 
  <tr>
    <td width=\"25%\"><p><strong>Comments:</strong></p></td>
    <td width=\"75%\"><p>$comments</p></td>
  </tr> 
</table>
";
}
}

?>
                </p>
                  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                    <?php
//we set variables that we will use to display field labels
$fnameLabel "First<br> Name * ";
$lnameLabel "Last<br> Name * ";
$emailLabel "Email * ";
$conameLabel "Company ";
$phoneLabel "Telephone * ";
$phone2Label "Alternate<br> Telephone ";
$addressLabel "Address * ";
$address2Label "Address ";
$cityLabel "City * ";
$provinceLabel "Province * ";
$postalcodeLabel "Postal<br> Code * ";
$commentsLabel "Comments ";

if (
$result == "") {
if (
$message) {
echo 
"$message";
}
$fname stripslashes($fname);
$lname stripslashes($lname);
$email stripslashes($email);
$coname stripslashes($coname);
$phone stripslashes($phone);
$phone2 stripslashes($phone2);
$address stripslashes($address);
$address2 stripslashes($address2);
$city stripslashes($city);
$province stripslashes($province);
$postalcode stripslashes($postalcode);
$comments stripslashes($comments);

echo 
"<p>Please fill in all the fields below.</p>

   <table width=98% align=left border=0 cellpadding=2>
    <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$fnameLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"fname\" value=\"
$fname\">
            </td>
    </tr>
     <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$lnameLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"lname\" value=\"
$lname\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$emailLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"email\" value=\"
$email\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$conameLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"coname\" value=\"
$coname\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$phoneLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"phone\" value=\"
$phone\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$phone2Label</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"phone2\" value=\"
$phone2\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$addressLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"address\" value=\"
$address\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$address2Label</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"address2\" value=\"
$address2\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$cityLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"city\" value=\"
$city\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$provinceLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"province\" value=\"
$province\">
            </td>
    </tr> <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$postalcodeLabel</p></div></td>
      <td width=80% valign=top>
        <input type=\"text\" size=\"30\" name=\"postalcode\" value=\"
$postalcode\">
            </td>
    </tr>
     <tr> 
      <td width=20% valign=top> 
        <div align=left><p>
$commentsLabel</p></div></td>
      <td width=80% valign=top>
     <textarea cols=\"20\" rows=\"4\" name=\"comments\">
$comments</textarea>
            </td>
    </tr>
    <tr><td colspan=\"2\">"
;?>
<table cellpadding=2 cellspacing=0 bgcolor="#CCCCCC">

<tr>

<td style="padding: 2px;" width="10"><img src="../../captcha/captchac_code.php" id="captcha"></td>

<td valign="top"><p><strong>Image Verification</strong><br />
  Please enter the text from the image<br><input type="text" name="Turing" value="" maxlength="100" size="10">
  <br />
  
  [<a href="#" onclick=" document.getElementById('captcha').src = document.getElementById('captcha').src + '?' + (new Date()).getMilliseconds()">Refresh Image</a>] [<a href="../../captcha/whatisturing.html" onClick="window.open('../..//captcha/whatisturing.html','_blank','width=400, height=300, left=' + (screen.width-450) + ', top=100');return false;">What's This?</a>] </p></td>

</tr>

</table>



    
<?php
echo"</td></tr>
       <tr> 
     <td valign=top>
     <br><input name='submit' type=submit value='Submit'>
   </td><td>&nbsp;</td>
    </tr>
  </table>

"
;
}

?>
                </form>
Here is the captcha code:
PHP Code:
<?php
// ----------------------------------------------------------------
// Captcha Creator PHP Image Displaying Script.
// Copyright (c) 2007 Alexandru Marias. All rights reserved
// Web: http://www.captchacreator.com
// Phone: +40722486348
// ----------------------------------------------------------------

 
session_start();

function 
RandomCode($min,$max// Chose the turing code
{
// Choosing a random Security Code
Global $CSrc;  // CSrc contains the characters from which the Captcha Code will be generated

$srclen strlen($CSrc)-1;

// Chose the length of the turing code
$length mt_rand($min,$max); // Between 4 and 8 chars

$Code '';

// Fill the turing string with characters and numbers from $src
for($i=0$i<$length$i++) 
    
$Code .= substr($CSrcmt_rand(0$srclen), 1);

return 
$Code;

}

// ----------------------------------------------------------------
function CheckCaptcha($Turing)


global 
$ImageCode;     

if ( 
session_id() == "" )
    {
    
// We have no session id. There is no captcha code stored anywhere
    // Return false
    
$ImageCode 'wrong'; return 0;
    }

if ( !isset( 
$_SESSION['turing_string'] ) ) 
    { 
    
// We have a session id, but the Turing String is empty, it was not generated yet.
    // Return true
    
$ImageCode ''; return 1
    }
    else if ( 
strtoupper($_SESSION['turing_string']) == strtoupper($Turing) ) 
            { 
            
// Session id is ok, and Generated captcha is identical to user entered captcha
            // Return true
            
$ImageCode 'ok'; return 1
            }
        else { 
$ImageCode 'wrong'; return 0; }
}
  


?>