Forum Moderators: coopster
<?php
// deal with recaptcha
if(isset($_POST['g-recaptcha-response'])){
//your site secret key
$secret = 'MY SECRET KEY GOES HERE';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if(isset($responseData->success) && $responseData->success === true){
// set email recipient
$myemail= 'info@EXAMPLE.com';
// check all form inputs using check_input function
$name = check_input($_POST['name'], 'Please enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$comment= check_input($_POST['comment'], 'Please add a note');
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
// prepare message for email delivery
$message = "Hello!
Your contact form has been submitted by:
Name: $name
Email: $email
Phone: $phone
Additional Comments from $name: $comment
End of message
";
/* redirect visitor to thank you page */
header('Location: /thankyou.html');
exit();
}
else {
/* functions used for cleaning and handling errors */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css_reset.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="mq-980-1199.css" media="(min-width:980px)">
<link rel="stylesheet" href="mq-1200-plus.css" media="(min-width:1200px)">
</head>
</head>
<body>
<p class="paragraph">Please correct the following errors</p>
<p class="paragraph"><?php echo $myError; ?></p>
</body>
</html>
<?php
exit();
}
}
}
?>
/* redirect visitor to thank you page */It's hard to tell from what's posted, but on a casual eyeballing it looks as if this header is being sent out while the user is already on the page--meaing that it isn't going to be sent out at all.
header('Location: /thankyou.html');
Also, the submitted form is not sent to the email address.Where in the code is it supposed to be sent? I can't find a "mail" command at all (admittedly not an easy search when this whole page is littered with "mail" and "email" text strings).
/* Send the message using mail() function */
mail($myemail, $subject, $message); ... it looks as if this header is being sent out while the user is already on the page--meaing that it isn't going to be sent out at all.
1. check recaptcha info. if success
2. receive info from form fields. if without error
3. send info to email address and load thank you page.
otherwise (if 1 or 2 not successful)
4. load error page
// deal with recaptcha
if(isset($_POST['g-recaptcha-response'])){
//your site secret key
$secret = 'MY SECRET KEY GOES HERE';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if(isset($responseData->success) && $responseData->success === true){
// set email recipient
$myemail= 'info@EXAMPLE.com';
// check all form inputs using check_input function
$name = check_input($_POST['name'], 'Please enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$comment= check_input($_POST['comment'], 'Please add a note');
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
// prepare message for email delivery
$message = "Hello!
Your contact form has been submitted by:
Name: $name
Email: $email
Phone: $phone
Additional Comments from $name: $comment
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* redirect visitor to thank you page */
header('Location: /thankyou.html');
exit();
}
else {
/* functions used for cleaning and handling errors */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css_reset.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="mq-980-1199.css" media="(min-width:980px)">
<link rel="stylesheet" href="mq-1200-plus.css" media="(min-width:1200px)">
</head>
</head>
<body>
<p class="paragraph">Please correct the following errors</p>
<p class="paragraph"><?php echo $myError; ?></p>
</body>
</html>
<?php
exit();
}
}
}
?>