Hi,
I'm creating a PHP form, and have some code that is supposed to check if fields are empty, and, if empty, display a message.
The code is behaving differently in Firefox and Internet Explorer.
The code starts with listing the variables that are expected and required, and storing them in an array; and an array for variables that are missing. I then have some logic that check if the variable is missing and required; if it is, it gets assigned to the missing array.
<?php
//process the email
if (array_key_exists('send', $_POST)) {
$to = 'cabusm@philau.edu';
$subject = 'Instruction Request';
//list expected fields
$expected = array('name', 'email', 'course', 'courselocation', 'daysmet', 'starttime', 'endtime', 'studentnumber', 'students', 'date1', 'date2', 'date3', 'classtime1', 'classtime2', 'classtime3', 'librarian', 'location', 'assignmentdate', 'assignment', 'outcomes', 'topic');
//required
$required = array('name', 'email', 'course', 'courselocation', 'daysmet', 'starttime', 'endtime', 'studentnumber', 'date1', 'date2', 'classtime1', 'classtime2', 'librarian', 'location', 'assignmentdate', 'outcomes');
//create empty array for any missing fields
$missing = array();
//process the post variables
foreach ($_POST as $key => $value) {
//assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
print($temp);
//if empty and required, add $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
//otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
If $missing has nothing in it, the message should send:
//build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Course: $course\n\n";
$message .= "Course Location: $courselocation\n\n";
$message .= "Days Met: $daysmet\n\n";
$message .= "Start time: $starttime\n\n";
$message .= "End time: $endtime\n\n";
$message .= "Student Number: $studentnumber\n\n";
$message .= "Students: $students\n\n";
$message .= "Date 1: $date1\n\n";
$message .= "Classtime: $classtime1\n\n";
$message .= "Date 2: $date2\n\n";
$message .= "Classtime: $classtime2\n\n";
$message .= "Date 3: $date3\n\n";
$message .= "Classtime: $classtime3\n\n";
$message .= "Librarian: $librarian\n\n";
$message .= "Location: $location\n\n";
$message .= "Assignment Date: $assignmentdate\n\n";
$message .= "Outcomes: $outcomes";
$message .= "Topic: $topic";
//limit the line length to 70 characters
$message = wordwrap($message, 70);
//send it
$mailSent = mail($to, $subject, $message);
}
}
Finally, I have some code that checks if the form has missing values, and displays an error message if so:
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class="warningInstruction">The form doesn't look complete! Please complete the missing item(s) indicated</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try later.</p>
<?php
}
else
if ($_POST && $mailSent) {
?>
<p><strong>Your message has been sent. Thank you for your feedback.
</strong></p>
<?php } ?>
What seems to be happening is, in IE, no matter if all the values are entered in the form, the form will not submit; it displays: The form doesn't look complete! Please complete the missing item(s) indicated. In Firefox, I don't think the code is being recognized at all, however it is detecting when fields are not complete after the submit button is pressed (a pop-up window indicates it hasn't submitted).
I'm going to do some error checking, but I'm baffled...is there a different way to do form validation?
Thanks for any help,
Michael