S
S
Shamil Khairulaev2021-04-15 22:05:57
AJAX
Shamil Khairulaev, 2021-04-15 22:05:57

Why is extra text (html markup) displayed in the alert?

After the ajax success request, the function should output "Working" (Which is returned by the php request handler). The very top line does indeed display what you need, but other than that, the html markup of one of my documents.

60788d7db1883660354158.png

Here is the ajax send code:

function change_description() {
   const penbtn = $('.mypage__change-descr');
   penbtn.click(function () {
      const changeForm = penbtn.parent().siblings('.mypage__write-descr').eq(0);
      const closeBtn = changeForm.find('.mypage__write-descr-header-close');
      const submit = closeBtn.siblings('.mypage__write-descr-header-submit');
      changeForm.slideToggle('fast');

      closeBtn.click(function () {
         changeForm.slideUp('fast');
      });

      submit.click(function () {
         const descrText = changeForm.children().eq(1).val();

         $.ajax({
            type: 'POST',
            // url: 'php-scripts/my-pageHandler.php',
            dataType: 'html',
            data: {
               descrText: descrText
            },
            success: function (data) {
               const descriptionBox = $('.mypage__description p');
               descriptionBox.text(descrText);
               changeForm.slideUp('fast');
               alert(data);
            }
         });
      });
   });
}


Ajax processing code in php:

function change_description()
{
   if (isset($_POST['descrText'])) {
      require __DIR__ . '/../#DATABASE.php';

      $newDescription =  htmlspecialchars($_POST['descrText']);

      $id = $_SESSION['user']['id'];

      $query = $db->prepare(
         "UPDATE `users` SET `description`=:description WHERE `id`='$id'"
      );
      $param = ['description' => $newDescription];
      $query->execute($param);
      echo 'Работает';
   }
}


Thanks in advance for your reply. I will answer all your questions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aricus, 2021-04-16
@Aricus

success: function (data) {
               ...
               alert(data);
            }

Here you display the html code of the ajax page in alert.
That is, the reason is in the php of the page php-scripts/my-pageHandler.php : for some reason, after the function is executed, it outputs something else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question