ajax — ошибка отправки формы html & amp; Переполнение стека

Я строю контактную форму php с проверкой ajax. Когда я отправляю контактную форму, я получаю сообщение об ошибке «spammer», которое не предполагается.

Ниже мой код HTML & PHP-код.

Index.php

<form id="contactform" method="post" action="mailer.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="username" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Enter your phone number">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email address">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" rows="4" placeholder="Enter your email message"></textarea>
</div>

<br>
<div class="g-recaptcha" data-sitekey="6Lct9zUUAAAAABpj9vwKX9B7x_AH8s9gwJzFW1sB"></div>
<br>
<div id="formresult">
</div>
<button type="submit" name="submit" class="btn btn-info">Submit</button>
</form>

Mailer.php

  <?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = trim($_POST["username"]);
$phone = trim($_POST["phone"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

if(isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}

//Validate the data
if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha)) {
http_response_code(400);
echo "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span> <strong>Please fill all the form inputs and check the captcha to submit.</strong>";
exit;
}

//recipient email address.
$recipient = "abc@abc.com";
$subject = "New message from $name";

//email content.
$email_content = "Name: $name\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
//email headers
$email_headers = "From: $name <$email>";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRATE_KEY&amp;response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$decoded_response = json_decode($response, true);

if($decoded_response['success'] == true){
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span> <strong>Thank You! Your message has been sent.</strong>";
} else {
http_response_code(500);
echo "Your message cannot be sent";
}
} else {
http_response_code(400);
echo 'spammer!';
}
}
?>

Когда я отправляю форму, я получаю сообщение об ошибке «spammer», и даже почта не проходит.

Я знаю, что в логике есть какая-то ошибка, но я не могу ее понять.

Кто-нибудь, пожалуйста, помогите мне в этом. Спасибо.

0

Решение

Строка кода:

if($decoded_response['success'] == true){

Должен быть оценен как ложный, и выполнить остальную часть этого утверждения. Отображение сообщения спамера.

Проверьте декодированное содержимое, возвращаемое из URL-адреса recaptcha, который вы присваиваете переменной $ decoded_response.

print_r($decoded_response); // should show the whole contents
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector