Ich mache ein Formular, ich benutze AJAX, um es zu senden, aber ich habe auch eine Google reCAPTCHA darin und ich weiß nicht, wie man es erforderlich macht.Wie wird ein reCAPTCHA benötigt?
Ich möchte das Formular nicht ohne checked reCAPTCHA senden und wenn es nicht aktiviert ist, dann möchte ich die Informationen darüber anzeigen. Ich habe viele Dinge ausprobiert, aber ich weiß immer noch nicht, was los ist.
Mein HTML-Code:
<div class="form">
<div id="form-messages"></div>
\t <form id="formularz" method="post" action="mailer.php">
<div class="line">
\t <input id="name" name="name" type="text" placeholder="Imię"/>
\t <input id="email" name="email" type="email" placeholder="E-mail" required/>
</div>
<div class="line2">
\t <input id="temat" name="temat" type="text" placeholder="Temat" />
\t <textarea id="wiadomosc" name="message" placeholder="Wiadomość" required></textarea>
</div>
<div class="line3">
\t <div class="g-recaptcha captcha" data-sitekey="6Lcu9xgUAAAAAPbwLwKWILHGxu-X0cPjAhRtYM2R"></div>
\t <input id="submit" name="submit" type="submit" value="Wyślij">
<div style="clear:both;"></div>
</div>
\t </form>
</div>
Mein js Code:
$(function() {
\t var form = $('#formularz');
var formMessages = $('#form-messages');
\t $(form).submit(function(e) {
\t \t e.preventDefault();
\t \t var formData = $(form).serialize();
\t \t $.ajax({
\t \t \t type: 'POST',
\t \t \t url: $(form).attr('action'),
\t \t \t data: formData
\t \t })
\t \t .done(function() {
\t \t \t $(formMessages).removeClass('error');
\t \t \t $(formMessages).addClass('success');
$(formMessages).text('Dziękujemy, wiadomość została wysłana.');
\t \t \t $('#name').val('');
\t \t \t $('#email').val('');
\t \t \t $('#temat').val('');
\t \t \t $('#wiadomosc').val('');
\t \t })
\t \t .fail(function() {
\t \t \t $(formMessages).removeClass('success');
\t \t \t $(formMessages).addClass('error');
\t \t \t $(formMessages).text('Oops! Wiadomość nie mogła zostać wysłana.');
\t \t });
\t });
});
Und PHP-Code:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
\t \t \t \t $name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$recipient = "[email protected]";
$subject = "Nowa wiadomość od $name";
$email_content = "Od: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Wiadomość:\n$message\n";
$email_headers = "Od: $name <$email>";
\t \t $wszystko_OK=true;
\t \t $sekret = "6Lcu9xgUAAAAAPbwLwKWILHGxu-X0cPjAhRtYM2R";
\t \t $sprawdz = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$sekret.'&response='.$_POST['g-recaptcha-response']);
\t \t $odpowiedz = json_decode($sprawdz);
\t \t if (!$odpowiedz->success){
\t \t \t $wszystko_OK=false;
\t \t \t http_response_code(600);
\t \t \t echo "Proszę zaznaczyć reCAPTCHE";
\t \t }
\t \t if($wszystko_OK==true){
\t \t \t mail($recipient, $subject, $email_content, $email_headers);
\t \t }
}
Mögliche Duplikat [Wie kann ich reCAPTCHA machen ein Pflichtfeld?] (Http://stackoverflow.com/questions/27706594/how-can-i-make-recaptcha-a-required-field) –
@Karol, überprüfen Sie dieses Thema, es könnte hilfreich sein - http://Stackoverflow.com/questions/29612879/google-recaptcha-how-to-make-required/29613089#29613089. – Viktor