I have been struggling with this all day long...The logic I have seems to be right, but something is not working out.
The form, if all fields pass the error checking tests, should be submitted to an SQL database. If one of the fields do not pass the error checking, the custom error message should be displayed, along with the form.
But, the form doesn't even display when I initially load the page!!
Basically, this is a form with error checking, an IF statment and and ELSE statement, but if anyone can point out anything I have not noticed in my logic, I'd be greatful!!!!
----------------CODE----------------------
<html>
<head>
<title>Luzerne County Live Search - Add a Profile</title>
</head>
<body>
<?php
#BEGIN THE DYNAMIC PHP FORM
$form ="<form action=\"$PHP_SELF\" method=\POST\">";
$form .="First Name:<input type=\"text\" name=\"firstname\" ";
$form .=" size=\"30\" value=\"$firstname\"><br><br>";
$form .="Last Name:<input type=\"text\" name=\"lastname\" ";
$form .=" size=\"30\" value=\"$lastname\"><br><br>";
$form .="<select name=\"department\">";
$form .="<option value='\$911\'>911</option>
<option value=\'$adult_probation\'>Adult Probation</option>
<option value='\$area_agency_on_aging\'>Area Agency on Aging</option>
<option value='\$assessors\'>Assessors</option>
<option value='\$benefits_coord\'>Benefits Coord</option>
<option value='\$buildings_and_grounds\'>Buildings and Grounds</option>
</select><br><br>";
$form .="Title:<input type=\"text\" name=\"title\" ";
$form .=" size=\"30\" value=\"$title\"><br><br>";
$form .="Phone:<input type=\"text\" name=\"phone\" ";
$form .=" size=\"30\" value=\"$phone\"><br><br>";
$form .="Extention:<input type=\"text\" name=\"extention\" ";
$form .=" size=\"30\" value=\"$extention\"><br><br>";
$form .="Fax:<input type=\"text\" name=\"fax\" ";
$form .=" size=\"30\" value=\"$fax\"><br><br>";
$form .="E-Mail:<input type=\"text\" name=\"email\" ";
$form .=" size=\"30\" value=\"$email\"><br><br>";
$form .="<input type=\"submit\" name=\"submit\" value=\"Enter\" ";
$form .="</form>";
#EXECUTE THIS CODE IF THE FORM HAS BEEN SUBMITTED ONCE
if ($submit)
{ $valid=true; #SET VARIABLE DEFAULT VALUE
#CHECK THAT FIRST NAME FIELD IS NOT BLANK
if (!$firstname)
{ $errmsg .="Enter your first name...<br>"; $valid=false; }
#CHECK THAT LAST NAME FIELD IS NOT BLANK
if (!$lastname)
{ $errmsg .="Enter your last name...<br>"; $valid=false; }
#CHECK THAT DEPARTMENT FIELD IS NOT BLANK
if (!$department)
{ $errmsg .="Choose a department...<br>"; $valid=false; }
#CHECK THAT THE TITLE FIELD IS NOT BLANK
if (!$title)
{ $errmsg .="Enter your title...<br>"; $valid=false; }
#CHECK THAT THE PHONE FILED IS NOT BLANK
if (!$phone)
{ $errmsg .="Enter your phone number...<br>"; $valid=false; }
#CHECK THAT THE EMAIL IS FORMATTED
if ($email)
{ $email = trim($email);
#PATTERNS FOR NAME, DOMAIN, AND TOP-LEVEL DOMAINS
$_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
$_host = "([-0-9A-Z]+\,)+";
$_tlds = "([0-9A-Z]){2,4}$/i";
#CHECK THE VALIDITY OF THE EMAIL FORMAT
if( !preg_match ($_name."@".$_host .$_tlds,$email) )
{$errmsg .="E-Mail address has an incorrect format!<br>"; $valid=false; }
}
#IF THE FORM IS INVALIDLY SUBMITTED, WRITE THE ERROR MESSAGE AND THE FORM
if ($valid=false)
{ echo($errmsg.$form); }
else
#IF THE FORM IS VALID, SUBMIT THE INFORMATION TO THE DATABASE
#CONNECT TO SQL
$conn = @mysql_connect("localhost","michael","xcrunner")
or die("Could not connect to MySQL: " .mysql_error());
#CONNECT TO THE DATABASE
$bd = @mysql_select_db("COUNTY_DIRECTORY",$conn)
or die("Could not select database: " .mysql_error());
#ENTER THE FORM INFORMATION
$sql = "insert into EMPLOYEE_INFO
(f_name,l_name,dept,title,phone,ext,fax,email) values
(\"$firstname\",\"$lastname\",\"$department\",\"$title\",\"$phone\",\"$extention\",\"$fax\",\"$email\" )";
#GIVE A RESULT
$result = @($sql,$conn)
or die("Could not enter information: " .mysql_error());
#IF RESULT, DISPLAY THE FOLLOWING MESSAGE
if ($result) { echo ("<br><br><h2><font face='Arial' color='#4d50a3'>New profile for <font color = 'black'>$firstname $lastname</font> added!</font></h2><br><font face='Arial'><br>
<b>Click <a href='default.htm'>HERE</a> to log-out.</b><br><br><b>Click <a href='admin_add.php'>HERE</a> to add another employee!</b><br><br><b>Click <a href='admin_edit_home.htm'>HERE</a> to return to administration home.</b></font>"); }
}
?>
</body>
</html>
I know it's a lot, but breaking it down, it's a form with error checking, and IF, and an ELSE statement.
I hope someone can honestly help me!!!!
TIA, Mike