@BlueNorp

Возвращает NULL в $_POST запросе,когда есть капча,и выбрано 2 файла на загрузку,почему?

Есть форма:
<form action="send_msg.php" enctype="multipart/form-data" method="POST">
<input type="text" name="email" required></br>
<textarea name="body" required></textarea></br>
<input type="file" name="file[]" multiple style="margin-bottom: 5px;"></br>
<div style="transform:scale(0.8); transform-origin:0; margin-left: 68px;" class="g-recaptcha" data-sitekey="MY_KEY" data-callback="captcha_onclick"></div>
<input type="submit" name="gsubmit" value="Отправить"></br>
</form>

И есть код обработки:
if (isset($_POST['g-recaptcha-response'])) $captcha_response = $_POST['g-recaptcha-response'];
else $captcha_response = $_POST['recaptcha'];
 
$url = 'https://www.google.com/recaptcha/api/siteverify';
$params = [
    'secret' => 'KEY',
    'response' => $captcha_response,
    'remoteip' => $_SERVER['REMOTE_ADDR']
];
 
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$response = curl_exec($ch);
if(!empty($response)) $decoded_response = json_decode($response);
 
$success = false;
 
if ($decoded_response && $decoded_response->success)
{
    $success = $decoded_response->success;
}
 
$result = $success ? 'Капча пройдена успешно!' : 'Неверная капча!';
echo $result;

Так вот,при отправки без файлов,капча проходит,как и вся форма,но если добавить две фотки,весь POST запрос становится null,что делать?
  • Вопрос задан
  • 275 просмотров
Решения вопроса 1
Ninazu
@Ninazu
Рекомендую начать с работающего примера, и потом усложнять его капчами и прочей логикой

<?php
if ($_POST) {
	die(json_encode([$_POST, $_FILES]));
}
?>

<form enctype="multipart/form-data" method="POST">
	<input type="text" name="name" required></br>
	<input type="file" name="file[]" multiple style="margin-bottom: 5px;">
	<input type="submit" name="gsubmit" value="Отправить"></br>
</form>
<script>
	document.querySelector("form").addEventListener("submit", function(e) {
		let form = e.target;
		e.preventDefault();
		e.stopImmediatePropagation();
		let xhr = new XMLHttpRequest();

		xhr.open(form.method, form.action);
		xhr.onreadystatechange = function(result) {
			if (xhr.readyState === 4 && xhr.status === 200) {
				let responseData = JSON.parse(xhr.responseText);
				console.log(responseData);
			}
		};

		xhr.send(new FormData(form));
	}, true);
</script>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы