Reputation: 59
I am very new to php and can't quite figure out what is wrong with this. Any help would be appreciated. Also, it would be beneficial to have a way to test whether it is me or just my hosting service that will not allow me to send the email. So, if there is a way that I could do that please let me know.
<?php
session_start();
$visitor_email = '';
$user_message = '';
//Validates the email address that the user entered
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
//When user clicks 'submit'
if (!isset($_POST['submit']))
{
$visitor_email = $_POST['email'];
$user_message = $_POST['message'];
//if user entered email address
if (isset($_POST['email']))
{
// Check if email address is valid
$mailcheck = spamcheck($visitor_email);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$to = "[email protected]";
$subject="Customer Feedback";
$from = $visitor_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A patron submitted the contact form:\n".
"Email: $visitor_email \n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
header('Location: home.html');
}
}
}
?>
<form id="feedBackForm" action="<?php echo htmlentities($_SERVER['PHP_SELF'])?>" method="post">
<fieldset id="personal">
<legend>Your Information</legend>
<label for="email">Contact Email: </label>
<input id="email" name="email" type="email" value='<?php echo htmlentities($visitor_email) ?>'/>
</fieldset>
<fieldset id="text">
<legend>Message:</legend>
<label for="message"></label>
<textarea id="message" name="message"><?php echo htmlentities($user_message) ?></textarea>
</fieldset><p><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset"/>
</p>
</form>
I have tried a couple different iterations and this is just where I happen to be at the moment
Upvotes: 1
Views: 70
Reputation: 74216
This if (!isset($_POST['submit']))
you're telling it "if it is not set, ....."
you want if(isset($_POST['submit']))
(telling it now, if it is set, execute everything inside the conditional statement)
N.B.: The !
is the logical not
operator.
Posted as requested by jeroen
Edit:
I tested your code after setting the correct conditional statement (removing the logical not
operator), and the mail was successfully sent and received. Therefore, you need to check if your server is set up to handle mail, or check your SPAM as well as server logs.
Upvotes: 5
Reputation: 12025
First I think you have to change
if (!isset($_POST['submit']))
to if (isset($_POST['submit']))
because it look like a bug
Upvotes: 1