PHP mail form help
I've cobbled together some php code to get a contact form together. Everything works but one thing. I'm trying to make the fields required and I must have missed a step. Right now the form just goes through even if you haven't filled anything out. Can anyone help? I've redacted a few bits for security purposes.
<?php
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'code goes here',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com........');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
// If CAPTCHA is successfully completed...
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: localhost';
$to = 'email@email.com';
$subject = 'RMA Request';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from))
header('Location:www.whatevertheaddresswillbe.html');
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
