I’m trying to create an array so that any errors encountered can be output at the bottom of a form. I am having two problems though. One is that I’m fairly certain the php side of the code is ignoring my “_submit_check” hidden value. The second (more important) issue is that the error array is not aggregating OR outputting when invalid form data is entered!
Could anyone please look over this script and let me know of any errors? (Be tough, this is my first PHP project and I learn by example)
<html>
<form method="post" action="search.php?find" id="searchform">
<input type="submit" name="add" value="Add an employee">
</form>
<?php
date_default_timezone_set('America/New_York'); //set for date functions
function realdate($date) {
if(preg_match("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $_POST['hiredate2'])){
$dateparts = explode("-", $date);
$mm=$dateparts[0];
$dd=$dateparts[1];
$yyyy=$dateparts[2];
if(!checkdate($mm, $dd, $yyyy)) {
$errors[]="$date is an invalid date. /r/n";
}
else {
$date=date('Y-d-m',strtotime($date));
return($date);
}
} else {
$errors[]="$date must be formatted as MM-DD-YYYY.";
}
}
//SELECT DEPARTMENT
if(isset($_POST['add'])){
if(isset($_GET['find'])){
{
echo "<b><u>Employee Information</u></b> <br><br>
<form name=\"jump\">
<select name=\"menu\" onChange=\"location=document.jump.menu.options[document.jump.menu.selectedIndex].value;\" value=\"GO\">
<option value=\"\">Select a Department</option>
<option value=\"?addadmin\">Administration</option>
</select>
</form>";
}}}
//ADD ADMIN USER
if(isset($_GET['addadmin'])){
if(isset($_POST['_submit_check'])){ //$_POST['_submit_check'] is a hidden field, makes redisplay possible
if($form_errors = validate_form()){
show_form($form_errors);
} else {
process_form();
}
} else {
show_form();
}}
function show_form($errors = '') {
// INITIATE FORM
print "<b><u>Employee Information</u></b><form method=\"post\" action=\"search.php?addadmin\" id=\"editform\"> <ul>
<table border=\"0\" width=\"550\">
include 'basicinfo.php';
<tr>
<td>Hire date:</td>
<td><input type=\"text\" name=\"hiredate2\"> (mm-dd-yyyy)</td>
</tr>
<tr>
<td>Orientation completed?</td>
<td><input type=\"checkbox\" name=\"orientation2\" value=\"Yes\" /></td>
</tr>
";
include 'basictrainings.php';
print "</table></ul>
<input type=\"submit\" name=\"submit2\" value=\"Submit\">
<input type=\"hidden\" name=\"_submit_check\" value=\"1\">
<input type=\"button\" name=\"Cancel\" value=\"Cancel\" onclick=\"window.location = 'search.php' \" />
</form>";
//output any errors
if ($errors){
print 'Please correct these errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
}
function validate_form(){
//VALIDATE
if(!preg_match("/^[A-Z|a-z]+$/", $_POST['lastname2'])){
$errors[]= "You must enter a valid last name.";
}
if(!preg_match("/^[A-Z|a-z]+$/", $_POST['firstname2'])){
$errors[]= "You must enter a valid first name.";
}
}
function process_form(){
//PROCESS FORM
if(isset($_POST['submit2'])){
$hiredate2=realdate($_POST['hiredate2']);
$firstname2=$_POST['firstname2'];
$lastname2=$_POST['lastname2'];
$position2=$_POST['position2'];
if (isset($_POST['orientation2'])) { // checkbox has been checked
$orientation2=$_POST['orientation2'];
} else {
$orientation2="No";}
$bestdate2=realdate($_POST['bestdate2']);
$deescdate2=realdate($_POST['deescdate2']);
$flsdate2=realdate($_POST['flsdate2']);
include 'db.php'; //connect to the database
include 'insert.php';
//$result=mysql_query($sql);
echo "<p>Entry created for " . $lastname2 . ", " . $firstname2 . "</p>";
echo "Tried to pass:" . $sql . ".";
}
}
?>
</html>