I am a PHP newb. I have the following code for a web form. It works fine as is, but I would like to do the following:
Return the errors as an array (?) so I can display errors as individual lines under each input.
and
Disallow the form from being able to be submitted twice.
Any help would be greatly appreciated.
<form id="form1" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<fieldset>
<legend>Contact Me</legend>
<?php
if (isset($_POST['Submit'])) {
if ($_POST['firstname'] != "") {
$_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
if ($_POST['firstname'] == "") {
$errors .= 'Please enter a valid first name.<br/><br/>';
}
} else {
$errors .= 'Please enter your first name.<br/>';
}
if ($_POST['lastname'] != "") {
$_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
if ($_POST['lastname'] == "") {
$errors .= 'Please enter a valid last name.<br/><br/>';
}
} else {
$errors .= 'Please enter your last name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}
if (!$errors) {
$mail_to = '***#****.com';
$subject = 'New Mail from Web Site';
$message = 'From: ' . $_POST['firstname'] . " " . $_POST['lastname'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= "Message:\n" . $_POST['message'] . "\n\n";
mail($mail_to, $subject, $message);
echo "<p>Thank you for your email!<br/><br/></p>";
} else {
echo '<div style="color: #00CC00">' . $errors . '<br/></div>';
}
}
?>
<label>First Name:</label>
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" size="50" />
<label>Last Name:</label>
<input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" size="50" />
<label>Email Address:</label>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/>
<label>Message:</label>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" class="moveright" name="Submit" value="Submit" />
</fieldset>
</form>
You can use an array for the errors instead of concatenating them into one string. Then you can check for each error at the specified form input.
Sample error check
// instead of: $errors .= 'Please enter a message to send.<br/>';
if ($_POST['message'] == "")
$errors['message'] = 'Please enter a message to send.<br/>';
Sample error display
<label>Message:</label>
<?php if ($errors['message'] != "") echo $errors['message']; ?>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
Instead of appending each error to the string, do like the following:
$errors[] = 'error text';
EDIT: as the others have said, it's good practice to initialize the array before starting to set the values, like so: $errors = array();
As for the disallowing the form to be submitted twice, that needs javascript. Here's a link to help: http://www.webmasterworld.com/forum91/3781.htm
To make your errors into an array, initialize it before form processing as:
$errors = array();
if (isset($_POST['Submit'])) {
...
Each time you have an error, rather than concatenating it on with .=, use the [] array append syntax:
$errors[] = 'Please enter a message to send.';
To prevent the form from being submitted twice, we often use a variable in $_SESSION to indicate that it has been completed. On successful submission, set a $_SESSION['success'] flag. Don't forget also to initialize the session at the start of the script:
session_start();
$_SESSION['success'] = FALSE;
$errors = array();
// Only process the form if the session flag isn't set:
if (isset($_POST['Submit']) && !$_SESSION['success']) {
...
// Later, on success,
echo "<p>Thank you for your email!<br/><br/></p>";
// Set the flag to prevent resubmission.
$_SESSION['success'] = TRUE;
Related
I have the following HTML:
<form method="post" action="https://www.domain.co.uk/v2/contact.php" id="contactform">
<p><label for="name"><span class="required">*</span> Your Name</label><br>
<input name="name" type="text" id="name" size="30" value="" class="input" required> </p>
<p><label for="email"><span class="required">*</span> Email</label><br>
<input name="email" type="email" id="email" size="30" value="" class="input" required></p>
<p><label for="phone">Phone</label><br>
<input name="phone" type="tel" id="phone" size="30" value="" class="input"></p>
<p><label for="subject">Subject</label><br>
<select name="subject" id="subject" class="input">
<option value="Not sure" selected="selected">Not sure</option>
<option value="this">this</option>
<option value="that">that</option>
</select></p>
<p><label for="comments"><span class="required">*</span> Message</label><br>
<textarea name="comments" cols="40" rows="3" id="comments" class="input" required></textarea></p>
<p><input type="submit" class="submit" id="submit" value="Submit"></p>
</form>
and the following in contact.php
<?php
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['phone'] != "") {
$_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
if ($_POST['phone'] == "") {
$errors .= 'Please enter your phone number.<br/>';
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['subject'] != "") {
$_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
if ($_POST['subject'] == "") {
$errors .= 'Please choose a subject.<br/>';
}
} else {
$errors .= 'Please choose a subject.<br/>';
}
if ($_POST['comments'] != "") {
$_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
if ($_POST['comments'] == "") {
$errors .= 'Please enter a message.<br/>';
}
} else {
$errors .= 'Please enter a message.<br/>';
}
if (!$errors) {
$mail_to = 'info#myemails.co.uk';
$subject = 'Enquiry';
$message .= 'Regarding: ' . $_POST['subject'] . "\n\n";
$message .= 'Name: ' . $_POST['name'] . "\n\n";
$message .= 'Email: ' . $_POST['email'] . "\n\n";
$message .= 'Phone: ' . $_POST['phone'] . "\n\n";
$message .= 'Message ' . $_POST['comments'] . "\n\n";
$success = mail($mail_to, $subject, $message, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
}
?>
no matter what I change or try I end up on a blank white page for contact.php instead of seeing the sent or failed message (having removed my javascript validation incase I was causing issue there), likewise there is nothing in the error log and despite having gone back over the code I can't spot the issue? Unsure if I have stared at it for too long and missing something obvious or there is a deeper problem?
Any pointers appreciated.
var_dump shows it is getting the information:
array(5) { ["name"]=> string(10) "Joe Bloggs" ["email"]=> string(16) "joe#anyemail.com" ["phone"]=> string(11) "07123456789" ["subject"]=> string(8) "Not sure" ["comments"]=> string(17) "test message here" }
You used string concatenation, but you didn't defined your variables before that, if you change your code, like this:
<?php
$errors = '';
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['phone'] != "") {
$_POST['phone'] = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
if ($_POST['phone'] == "") {
$errors .= 'Please enter your phone number.<br/>';
}
} else {
$errors .= 'Please enter your phone number.<br/>';
}
if ($_POST['subject'] != "") {
$_POST['subject'] = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
if ($_POST['subject'] == "") {
$errors .= 'Please choose a subject.<br/>';
}
} else {
$errors .= 'Please choose a subject.<br/>';
}
if ($_POST['comments'] != "") {
$_POST['comments'] = filter_var($_POST['comments'], FILTER_SANITIZE_STRING);
if ($_POST['comments'] == "") {
$errors .= 'Please enter a message.<br/>';
}
} else {
$errors .= 'Please enter a message.<br/>';
}
$message = '';
if (empty($errors)) {
$mail_to = 'info#myemails.co.uk';
$subject = 'Enquiry';
$message .= 'Regarding: ' . $_POST['subject'] . "\n\n";
$message .= 'Name: ' . $_POST['name'] . "\n\n";
$message .= 'Email: ' . $_POST['email'] . "\n\n";
$message .= 'Phone: ' . $_POST['phone'] . "\n\n";
$message .= 'Message ' . $_POST['comments'] . "\n\n";
$success = mail($mail_to, $subject, $message, "From: <$email>");
if ($success){
print "sent";
}
else{
print "failed";
}
} else {
echo $errors;
}
?>
It works. You said that you are at live site, so probably errors are not show up, if you want you can add this lines:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
at the top of your php file, to see errors, but clients also would see these errors. You can add IP Check for your IP around them.
I am trying to create a simple PHP guestbook where a user enters their information, the information is then validated and written to a text file.
I was able to get the information the user enters written to the text file, but then I am having trouble echoing it out to the page I want since all the information spits out as one line.
So the user enters the information in form.php and I want to echo it to index.php. I want to echo out the name and message of each entry, not the email, on separate lines in almost a blog-like format, this way it shows like a guestbook. Right now I have the information in the text file, on different lines but I only want to select a few on the lines, not every single one (in this case the first and third line of each entry).
And also is there a way to include the time of each entry on index.php as well?
*It should be done without a database.
form.php
<h1>My Guestbook</h1>
<p>View Guestbook</p>
<p class="divider">|</p>
<p>Leave a Message</p>
<form name="form" class="" action="form.php" method="post">
<label for="">
<h5>Name</h5>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" placeholder="Name">
<br>
</label>
<label for="">
<h5>Email</h5>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" placeholder="Email">
<br>
</label>
<label for="">
<h5>Message</h5>
<textarea name="message" rows="8" cols="40" value="<?php echo $_POST['message']; ?>" placeholder="message"></textarea>
<br>
</label>
<br>
<input class="submit" type="submit" name="submit" value="Submit">
</form>
<p>
<?php
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/>';
}
}
else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter your message.<br/>';
}
}
else {
$errors .= 'Please enter your message.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
}
else {
$errors .= 'Please enter your email address.<br/>';
}
if (!$errors) {
$mail_to = 'me#somewhere.com';
$subject = 'Email from Form';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= "message:\n" . $_POST['message'] . "\n\n";
mail($to, $subject, $message);
$guests = fopen('guests.txt', 'a+')
OR die ("Can't open file\n");
fwrite ($guests, $_POST["name"] . "\n");
fwrite ($guests, $_POST["email"] . "\n");
fwrite ($guests, $_POST["message"] . "\n");
fclose($guests);
header('Location: thank-you.php');
exit;
}
else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
?>
</p>
index.php
<h1>My Guestbook</h1>
<p>View Guestbook</p>
<p class="divider">|</p>
<p>Leave a Message</p>
<p class="rsps">
<?php
$guests = fopen("guests.txt", "r") or die("Unable to open file!");
echo fread($guests,filesize("guests.txt"));
fclose($guests);
?>
</p>
There must be thousands of questions asked about PHP email scripts but I still haven't found my answer. I looked on the php website but it didn't really help either.
Basically my script validates the inputs and displays error message accordingly but when I add the second part of the script to handle the sending of the email the page displays a 500 error. Here is my code. The email hasn't been set to the correct email at the moment but I know that's not the problem.
<?php include('includes/header.php');
$yourEmail = "hello#example.com";
$formSubject = "Message recieved from enquiry form";
if( isset( $_POST["submit"])) {
//Validate the data
function validateFormData( $formData ) {
$formData = trim( stripslashes(htmlspecialchars( $formData )));
return $formData;
}
//check to see if inputs are empty
// create variables with form data
// wrap the data with our function
if( !$_POST['name']) {
$nameError = "Please enter your name <br />";
} else {
$name = validateFormData( $_POST["name"]);
}
if( !$_POST['email'] || !preg_match("/^\S+#\S+$/", $email)) {
$emailError = "Please enter a valid email address <br />";
} else {
$email = validateFormData( $_POST["email"]);
}
if( !$_POST['enquiry']) {
$enquiryError = "Please enter your enquiry <br />";
} else {
$enquiry = validateFormData( $_POST["enquiry"]);
}
Code Works fine up until this point;
//Check to see if submit is set
//Check to make sure errors don't exist
//Send the email
// Redirect user to success.html
if ( isset( $_POST["submit"]) && (empty($emailError, $nameError, $enquiryError))) {
$to = $yourEmail;
$subject = $formSubject;
$headers = 'From:'. $name . "\r\n".
'Reply-To: '. $email . "\r\n".
'X-Mailer: PHP/' . phpversion();
$body =
'The person that contacted you was '.$name.'
Email: '.$email.'
URL: '.$url.'
Message: '.$enquiry.''.
mail($yourEmail, $formSubject, $body, $headers);
header("Location: success.html");
}
}
?>
This is the HTML form, which I'm sure is alright considering the PHP works up until a certain point and displays the error messages correctly. I know I have PHP_SELF which a lot of people say not to use but I want to to stay on the page if errors exist and only redirect if email has sent.
<form id="contactForm" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label for="name">Name <span class="asterisk">*</span></label>
<input type="text" name="name" id="name" value="<?php echo $name;?>"tabindex="1"/>
<span class="error"><?php echo $nameError; ?></span>
<label for="email">Email <span class="asterisk">*</span></label>
<input type="email" name="email" id="email" value="<?php echo $email;?>" tabindex="2"/>
<span class="error"><?php echo $emailError; ?></span>
<span class="error"><?php echo $emailValidError; ?></span>
<label for="url">Website</label>
<input type="url" name="url" id="url" value="<?php echo $url;?>" tabindex="3"/>
<h3>Tell us about your project </h3><br />
<textarea name="enquiry" id="enquiry" tabindex="4"><?php echo $enquiry;?></textarea>
<span class="error"><?php echo $enquiryError; ?></span>
<button type="submit" id="submit" name="submit" value="submit" >Submit</button>
</form>
Your problem is here:
empty($emailError, $nameError, $enquiryError)
empty accepts only one param, you are passing it 3. Change the conditional to:
if( isset( $_POST["submit"]) && ( empty($emailError) && empty($nameError) && empty($enquiryError) ))
Or if you want to shorten that conditional a little
$Errors = $emailError.$nameError.$enquiryError;
if( isset( $_POST["submit"] ) && empty( $Errors ) )
This is why there's an error. First syntax error and then (empty($emailError, $nameError, $enquiryError)) is not a good idea. I would do something like this:
$err=0;
if( !$_POST['name']) {
$nameError = "Please enter your name <br />";
$err++;
} else {
$name = validateFormData( $_POST["name"]);
$err++;
}
if( !$_POST['email'] || !preg_match("/^\S+#\S+$/", $email)) {
$emailError = "Please enter a valid email address <br />";
$err++;
} else {
$email = validateFormData( $_POST["email"]);
}
if( !$_POST['enquiry']) {
$enquiryError = "Please enter your enquiry <br />";
$err++;
} else {
$enquiry = validateFormData( $_POST["enquiry"]);
}
if ( isset( $_POST["submit"]) && $err==0) {
//your code here//
//and i would do like this//
$result_mail_send = mail($yourEmail, $formSubject, $body, $headers);
if($result_mail_send==true){
//redirect to success
}else{
//redirect to fail//
}
}
My contact form is sending black emails every time I visit the page, and the sends another whenever someone fills out the form and clicks send. I am trying to learn how to validate data for my contact form and have had little luck all day. I know very little php or javascript code as of yet but have done my best from previously written code.
I tried to use this http://www.inmotionhosting.com/support/edu/website-design/using-php-and-mysql/how-to-create-a-custom-php-contact-form to help with code.
Any ideas?
<form action="" method="post" style="width:100%;">
Name*<br> <input type="text" name="name" style="width:50%;" ><br/><br/>
Email*<br> <input type="text" name="email" style="width:50%;"> <br/><br/>
Phone*<br> <input type="text" name="phone" style="width:50%"> <br/><br/>
<input name="submitted" type="submit" value="Send" >
<input name="" type="reset" value"Reset">
</form>
<?php
if (isset($_REQUEST['submitted'])) {
// Initialize error array.
$errors = array();
// Check for a name
if (!empty($_REQUEST['name'])) {
$name = $_REQUEST['name'];
$pattern = "/^[a-zA-Z0-9\_]{2,20}/";]
if (preg_match($pattern,$name)) {
$name = $_REQUEST['name'];
} else {
$errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';
}
} else {
$errors[] = 'You forgot to enter your name.';
}
//Check for a valid phone number
if (!empty($_REQUEST['phone'])) {
$phone = $_REQUEST['phone'];
$pattern = "/^[0-9\_]{7,20}/";
if (preg_match($pattern,$phone)) {
$phone = $_REQUEST['phone'];
} else {
$errors[] = 'Your phone number can only be numbers.';
}
} else {
$errors[] = 'You forgot to enter your phone number.';
}
//Check for a valid email
if (!filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'The email provided is not valid.';
}
} else {
$email = $_REQUEST['email'];
}
//End of validation
if (empty($errors)) {
$from = "From: " . $_REQUEST['email'];
$to = "myemail#myemail.com";
$subject = "A comment from " . $name . "";
$message = "Message from " . $name . "
Phone: " . $phone . "
Email: " . $_REQUEST['email'] . "";
mail($to,$subject,$message, $from);
}
//Print Errors
if (isset($_REQUEST['submitted'])) {
// Print any error messages.
if (!empty($errors)) {
echo '<hr /><h3 >The following occurred:</h3><ul>';
// Print each error.
foreach ($errors as $msg) {
echo '<li>'. $msg . '</li>';
}
echo '</ul><h3 >Your mail could not be sent due to input errors.</h3><hr />';
} else {
echo '<hr /><h3 align="center" >Your mail was sent. Thank you!</h3><hr /><p>Below is the message that you sent.</p>';
echo "Message from " . $name . "<br />Phone: ".$phone."<br />Email:" . $email;
}
}
//End of errors array
?>
You're sending mail outside the if (isset($_POST['submitted'])) block. I think the problem is that you have an extra close brace here:
//Check for a valid email
if (!filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'The email provided is not valid.';
} // <<===== HERE
} else {
$email = $_REQUEST['email'];
}
As a result, the else clause isn't connected to the email validation, it's connected to if (isset($_POST['submitted'])). So when the form hasn't been submitted, you set $email and then you go into the code that sends email.
An array that hasn't been declared is still empty.
change
if (empty($errors)) {
to
if (isset($errors) && empty($errors)) {
In the following code the email sanitizing and validation:
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
is allowing:
ck#////bushidodee/xom
New to filters, and don't get why this is not sanitized?
Full Code:
if (isset($_POST['Submit'])) {
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
if ($_POST['homepage'] != "") {
$homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);
if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
$errors .= "$homepage is <strong>NOT</strong> a valid URL.<br/><br/>";
}
} else {
$errors .= 'Please enter your home page.<br/>';
}
if ($_POST['message'] != "") {
$_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($_POST['message'] == "") {
$errors .= 'Please enter a message to send.<br/>';
}
} else {
$errors .= 'Please enter a message to send.<br/>';
}
if (!$errors) {
// $mail_to = 'me#somewhere.com';
// $subject = 'New Mail from Form Submission';
// $message = 'From: ' . $_POST['name'] . "\n";
// $message .= 'Email: ' . $_POST['email'] . "\n";
// $message .= 'Homepage: ' . $_POST['homepage'] . "\n";
// $message .= "Message:\n" . $_POST['message'] . "\n\n";
// mail($to, $subject, $message);
print_r($_POST);
echo "Thank you for your email!<br/><br/>";
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
?>
<form name="form1" method="post" action="form-email.php">
Name: <br/>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" /><br/><br/>
Email Address: <br/>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><br/>
Home Page: <br/>
<input type="text" name="homepage" value="<?php echo $_POST['homepage']; ?>" size="50" /> <br/><br/>
Message: <br/>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" value="Submit Form Data" />
</form>
Hm, I do not get your problem, you first sanitize the input from $_POST and store it in $email if you print that var you will see it is ck#bushidodeexom and then you validate the sanitized input -- of course it passes.
Try this...
<?php
if ($_POST['email'] != ""){
$_POST['email'] = stripslashes(trim($_POST['email']));
$tmpEmail=filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if ( filter_var($tmpEmail, FILTER_VALIDATE_EMAIL) == TRUE) {
}
else{
$errors .= "Invalid Email";
}
}
else{
$errors .= "Please enter email";
}
?>