首页 > 解决方案 > 如何通过 ajax 正确地将数据通过 php 发送到电报?

问题描述

我在 JS 上为网站做了一份问卷调查。JS 运行良好,但无法通过 ajax 向电报发送数据。发送时,它会遇到错误 400。如果您将所有变量设置为“硬”,而不是像 $the =question 6:; 这样的帖子。然后一切正常。事实证明我传输数据不正确。告诉我如何正确地做到这一点。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");

标签: javascriptphpajaxtelegram

解决方案


推荐阅读