Сделал опросник для сайта на JS. JS отрабатывает хорошо, но не получается правильно передать данные через ajax в телеграм. При отправке бьет ошибку 400. Если задать все переменные "жестко" а не с пост типо $tel =
qwestion6:; то все норм отправляет. Получается Я не правильно передаю данные. Подскажите как правильно сделать.
Js до отправки отрабатывает как положено. Все на POST тухнет(хотя пишет что все отправлено успешно)
function compileForm() {
// take answers
var answerInput1 = $('.qwestion-1:checked'),
answerInput2 = $('.qwestion-2:checked'),
answerInput3 = $('.qwestion-3:checked'),
answerInput4 = $('.qwestion-4:checked'),
answerInput5 = $('.qwestion-5:checked');
answerInput3Custom = $('#qwestion-3-custom');
scoreSum = answerInput1.data('cost') + answerInput2.data('cost') + answerInput3.data('cost') + answerInput4.data('cost') + answerInput5.data('cost');
var answer1 = answerInput1.val(),
answer2 = answerInput2.val(),
answer3 = answerInput3.val(),
answer4 = answerInput4.val(),
answer5 = answerInput5.val();
if (answerInput3.hasClass('qwestion-3-custom-choose')) {
console.log('another');
answer3 += ' - ' + answerInput3Custom.val();
}
// put unswers to inputs
var sendInp1 = $('#answer-1').val(answer1),
sendInp2 = $('#answer-2').val(answer2),
sendInp3 = $('#answer-3').val(answer3),
sendInp4 = $('#answer-4').val(answer4),
sendInp5 = $('#answer-5').val(answer5);
sendInp6 = $('#answer-6').val(scoreSum);
};
$('#final-qwestion__btn').click(function(e) {
e.preventDefault();
var answers = [];
$('.qwestion').each(function(index, el) {
var number = index + 1;
if (index > 4) {
answers.push(number + ') Номер телефона —— ' + $(this).find('#qwestion__tel-input').val() + '')
answers.push(number + ') Город : —— ' + $(this).find('#qwestion__gorod-input').val() + '')
} else if (index == 2) {
if ($(this).find('input:checked').hasClass('other')) {
answers.push(number + ') ' + $(this).find('.qwestion__heading').text() + ' —— ' + $(this).find('#qwestion-3-custom').val() + '')
} else {
answers.push(number + ') ' + $(this).find('.qwestion__heading').text() + ' —— ' + $(this).find('input:checked').val() + '')
}
} else {
answers.push(number + ') ' + $(this).find('.qwestion__heading').text() + ' —— ' + $(this).find('input:checked').val() + '')
}
});
if (!$('#qwestion__tel-input').val()) {
$('#qwestion__tel-input').css('box-shadow', '0 0 20px 0px #990033');
} else {
$('#qwestion__tel-input').css('box-shadow', 'none');
answers = answers.join('\n')
console.log(answers);
$.ajax({
url: 'send/send.php',
type: 'POST',
data: {
postData: answers
},
success: function(resultData){
alert("Save Complete");
}
})
}
});
});
<?php
if (isset($_POST['qwestion-1'])) {
$qwestionq = strip_tags($_POST['qwestion-1']);
$qwestionqFieldset = "<b>qwestion1:</b> ";
}
if (isset($_POST['qwestion-3'])) {
$qwestiona = strip_tags($_POST['qwestion-3']);
$qwestionaFieldset = "<b>qwestion2:</b>";
}
if (isset($_POST['qwestion-4'])) {
$qwestioz = strip_tags($_POST['qwestion-4']);
$qwestionzFieldset = "<b>qwestion3:</b> ";
}
if (isset($_POST['qwestion-5'])) {
$qwestionw = strip_tags($_POST['qwestion-5']);
$qwestionwFieldset = "<b>qwestion4:</b> ";
}
if (isset($_POST['qwestion-6'])) {
$qwestions = strip_tags($_POST['qwestion-6']);
$qwestionsFieldset = "<b>qwestion5:</b> ";
}
if (isset($_POST['gorod'])) {
$gorod = strip_tags($_POST['gorod']);
$gorodFieldset = "<b>qwestion6:</b> ";
}
if (isset($_POST['tel2'])) {
$tel = strip_tags($_POST['tel2']);
$telFieldset = "<b>qwestion7:</b> ";
}
$token = "";
$chat_id_scr = "";
$txt = rawurlencode($txt);
$arr = array(
$qwestionqFieldset => $qwestionq,
$qwestionaFieldset => $qwestiona,
$qwestionzFieldset => $qwestionz,
$qwestionwFieldset => $qwestionw,
$qwestionwFieldset => $qwestions,
$gorodFieldset => $gorod,
$telFieldset => $tel,
);
foreach($arr as $key => $value) {
$txt .= "<b>".$key."</b>".$value."%0A";
};
$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id_scr}&parse_mode=html&text={$txt}","r");
Скрипт отрабатывает правильно и вызов лога выдает верный результат