Reputation: 59
I have searched here and in google how to make a contact form to work and I havent been able.
I downloaded a "php contact form" but I dont know how to make it work. I know there have been a lot asked question about this but please help me out. I am stuck here :/
This is the HTML code:
<form action="form-to-email.php" name="myemailform" method="post">
<div>
<span>Name</span>
<input type="text" name="name" value="" placeholder="Your Name">
</div>
<div>
<span>Email</span>
<input type="email" name="email" autocapitalize="off" autocorrect="off" value="" placeholder="[email protected]">
</div>
<div><textarea name="message" placeholder="Message"></textarea></div>
<div class="code">
<button><input type="submit" name='submit' value="Send"></button>
</div>
<i class="clear" style="display: block"></i>
</div>
</form>
And here is the "free PHP form code" I downloaded from a website:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = '[email protected]';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "[email protected]";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
Do I need to do something in mysql or do something more at all? Thanks for the help!
Upvotes: 0
Views: 940
Reputation: 74232
Here "Here is the message:\n $message".
<= see the dot? That's supposed to be a semi-colon.
That, is the reason why OP's code is not working.
Add error reporting to the top of your file(s) which will help find errors, which it would have.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Upvotes: 1
Reputation: 8819
everything looks correct, just add exit after redirect.
header('Location: thank-you.html');
exit;
Upvotes: 1