Form validation problem (newbie)
Posted: Mon Oct 11, 2010 12:31 pm
I have created a simple e-mail form consisting of three fields; name, e-mail address and the message itself.
The validation code works perfectly in Opera (v.10.62), but I hit a problem in both Firefox (v.3.6.10) and IE (v.7). The code checks to see if any fields are empty (all are required), and checks if the e-mail address is a valid address.
The contact.php file:
The problem occurs when I repeatedly hit 'Submit' in Firefox and IE without even filling in any of the fields. The first few times it works, and the error message is displayed, but then it looks like the line "if (isset($_POST['email]))" suddenly returns false and the form is displayed as if 'Submit' hasn't been clicked. However as I mentioned, this isn't an issue in Opera.
Any help is appreciated.
The validation code works perfectly in Opera (v.10.62), but I hit a problem in both Firefox (v.3.6.10) and IE (v.7). The code checks to see if any fields are empty (all are required), and checks if the e-mail address is a valid address.
Code: Select all
<?php
if (isset($_POST['email']))
{//if "email" is filled out, proceed
$email = $_POST['email'] ;
$name = $_POST['name'];
$message = $_POST['message'] ;
$emptyfields = FALSE;
// Are there any empty fields?
if (trim($email)=="" || trim($name)=="" || trim($message)=="")
{
$emptyfields = TRUE;
}
$validemail = validateaddress($email);
if ($emptyfields==TRUE)
{
echo "<p style=\"color:red\">An error occured:<br />- All the fields have to be filled in.</p>";
include("phpinclude/contact.php");
}
elseif ($validemail==FALSE)
{
echo "<p style=\"color:red\">An error occured:<br />- Invalid e-mail address.</p>";
include("phpinclude/contact.php");
}
else
{// Send e-mail
$subject = "Web e-mail";
mail_utf8("myaddress@somedomain.com", $subject,
$message, "From: $name <$email>" );
echo "Thank you.";
}
}
else
{// If the user has not yet submitted the form, display the form.
?>
<p>
Please send me an e-mail using the following form.
</p>
<?php
$email = "";
$name = "";
$message = "";
include("phpinclude/contact.php");
}
?>
Code: Select all
<form method='post' action='index.php?page=contact>
<table>
<tr><td>Name:</td><td><input type="text" name="name" size="40" value="<?php echo $name ?>" /></td></tr>
<tr><td>E-mail:</td><td><input type="text" name="email" size="40" value="<?php echo $email ?>" /></td></tr>
<tr><td>Message:</td><td><textarea rows="10" cols="60" name="message"><?php echo $message ?></textarea></td></tr>
<tr><td></td><td><input type="submit"></td></tr>
</table>
</form>
Any help is appreciated.