I am teaching myself PHP.
I am trying to write a contact form and thought I had it worked out except my footer file is not viewing properly in any browser after adding a textarea field. I thought I found a fix but now my validation is not working. After working on this for 3 days I am absolutely blind. Validating it at W3C shows it's fine in XHTML 1.0. Could someone look at my code and see where I went wrong?
<?php
//start output buffering
ob_start();
// Address error handling.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
// Set the page title and include the header file.
define ('TITLE', 'THE ACME WIDGET COMPANY');
require ('header.php');
//require the nav html file
require("nav.htm");
print '<div id="content">
<h3>Contact Form</h3>';
// Check if the form has been submitted.
if ( isset ($_POST['submit'])) {
$problem = FALSE; // Contact Form has no problems so far.
// Check for each value on contact form data.
if (empty ($_POST['first_name'])) {
$problem = TRUE;
print '<p>Please enter your first name.</p>';
}
if (empty ($_POST['last_name'])) {
$problem = TRUE;
print '<p>Please enter your last name.</p>';
}
if (empty ($_POST['user_email'])) {
$problem = TRUE;
print '<p>Please enter your email address.</p>';
}
if (empty ($_POST['subject'])) {
$problem = TRUE;
print '<p>Please enter a subject.</p>';
}
if (empty ($_POST['comments'])) {
$problem = TRUE;
print '<p>Please enter your comments or questions.</p>';
}
if (!$problem) { // If there weren't any problems with form data
// Send the email confirmation to user
$body = "Thank you for contacting THE ACME WIDGET COMPANY
Thank you for your email. Here is a confirmation of what you sent us. We will be contacting you shortly.
Your name is {$_POST['first_name']} {$_POST['last_name']}.
Your subject is {$_POST['subject']}.
Your comments were: {$_POST['comments']}.";
mail ($_POST['user_email'], 'ACME Contact Email Confirmation', $body, 'From: [email]name@host.com[/email]');
} else { // Forgot a field.
print '<p>Please try again. You have missed a field.</p>';
}
// Begin Session
session_name('ContactForm');
session_start();
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['user_email'] = $_POST['user_email'];
$_SESSION['subject'] = $_POST['subject'];
$_SESSION['comments'] = $_POST['comments'];
// Redirect the user to the thanks page
header ('Location: thanks.php');
exit();
} // End of handle form data IF.
?>
<?php
// Display the contact form.
print '<form action="contact.php" method="post">';
print '<table width="400">
<tr>
<td class="nc" width="176">First Name:</td><td class="nc" width="176"><input type="text" name="first_name" size="40" value="' . $_POST['first_name'] . '" /></td></tr>
<tr><td class="nc" width="176">Last Name:</td><td class="nc" width="176"><input type="text" name="last_name" size="40" value="' . $_POST['last_name'] . '" /></td></tr>
<tr><td class="nc" width="176">Email Address:</td><td class="nc" width="176"><input type="text" name="user_email" size="40" value="' . $_POST['user_email'] . '" /></td></tr>
<tr><td class="nc" width="176">Subject:</td><td class="nc" width="176"> <input type="text" name="subject" size="40" value="' . $_POST['subject'] . '" /></td></tr>
<tr><td class="nc" width="176">Comments:</td><td class="nc" width="176"><textarea rows="8" cols="30" name="comments">';
?>
<?php
echo (isset($_POST['comments'])) ? htmlspecialchars($_POST['comments']) : '';
print '</textarea></td></tr> </table>
<p><input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" /></p></form></div> </div>';
//require the footer html file
require("footer.php");
//send contents of buffer to the browser
ob_end_flush(); ?>