Answer the question
In order to leave comments, you need to log in
Conflict of two JSONs or why this error?
Hello.
The usual form where you need to perform an action upon successful completion of ReCaptcha and display a message about successful submission or error. But apparently there is a JSON conflict,
Error:
syntax error unexpected token in json at position 0
<script>
$(document).ready(function() {
$("#feedback").submit(function(){
var form = $(this);
var error = false;
form.find('input, textarea').each( function(){
if ($(this).val() == '') {
alert('Зaпoлнитe пoлe "'+$(this).attr('placeholder')+'"!');
error = true;
}
});
if (!error) {
var data = form.serialize();
$.ajax({
type: 'POST',
url: 'send.php',
dataType: 'json',
data: data,
beforeSend: function(data) {
form.find('input[type="submit"]').attr('disabled', 'disabled');
},
success: function(data){
if (data['error']) {
alert(data['error']);
} else {
alert('Письмo было отправлено, проверьте почту.');
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
},
complete: function(data) {
form.find('input[type="submit"]').prop('disabled', false);
}
});
}
return false; // вырубaeм стaндaртную oтпрaвку фoрмы
});
});
</script>
<?php
if ($_POST) {
// ReCaptcha Start
$secret = 'здесь мой секретный ключ от reCapctha';
$ip = $_SERVER['REMOTE_ADDR'];
$response = $_POST['g-recaptcha-response'];
$rsp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$ip");
$arr = json_decode($rsp, TRUE);
if($arr['success']){ // ReCaptcha проверка на успех ...
$author = $_POST['author'];
$author = addslashes($author);
$author = htmlspecialchars($author);
$author = stripslashes($author);
$author = mysql_real_escape_string($author);
$message = $_POST['message'];
$message = addslashes($message);
$message = htmlspecialchars($message);
$message = stripslashes($message);
$message = mysql_real_escape_string($message);
$date = date("d-m-Y в H:i:s");
$result = mysql_query("INSERT INTO messages (author, message, date) VALUES ('$author', '$message', '$date')");
}
// Обработка ошибок и вывод
$json = array();
if (!$name or !$author or !$message) {
$json['error'] = 'Вы зaпoлнили нe всe пoля.';
echo json_encode($json);
die();
}
$json['error'] = 0;
echo json_encode($json);
} else {
echo 'У вас нет прав для входа на эту страницу!';
}
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question