Why is ajax not getting any response back from Php? [duplicate]

So I am sending a form inputs directly to my Telegram bot using ajax and php. Everythings works just fine expect that I dont get the message I echo from php in ajax it is just empty.

Here is my main part from mail.php

<?php
$message = "";
if (isset($_POST["email"])) {
    $subject = $_POST['subject'];
    $email = $_POST['email'];
    $msg = $_POST['message'];
    $splitmail = explode("@", $email);
    $username = $splitmail[0];
    if (telegram($subject, $email, $msg, $username)) {
        $message = "You message has been sent!";
    } else {
        $message = "Please try again later!";
    }
}
echo $message;
?>

The telegram() function is responsible for sending the message which works without any problem. The problem is not even in php as the message is echoed back. Just the ajax is not working.

Here is the main part of my script.js

let res = "";
$.ajax({
            type: "POST",
            url: 'assets/php/mail.php',
            dataType: "json",
            data: send_data,
            success: function(response) {
                res = response;
            }
        });
        alert(res);

The alert(res) is just empty. I can't quite figure out the problem here.



Read more here: https://stackoverflow.com/questions/68460618/why-is-ajax-not-getting-any-response-back-from-php

Content Attribution

This content was originally published by verna at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: