PHP (or something else) adding unwanted HTML to my output

Viewed 42

I don't recall ever having this problem but think I've seen it pop up in other web apps, and I've worked in PHP and Ajax plenty of times, so I'm unsure if this is a PHP or browser issue. This is a repost with editing because apparently I lacked clarity.

I have a PHP file, that is literally just:

<?php echo 'Hello'; ?>

However when loaded in a browser, the actual browser source is

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
Hello
</body>
</html>

I am viewing the browser source code, not the inspector. If I set the header to plain text, then it does nothing but make the source code visible as plaintext.

This is a particular issue when I'm trying to call the file via Ajax, as the responseText is the full HTML string when I just want the word "Hello". Tested in Chrome, Opera, Edge & Firefox

The Javascript/Jquery request:

function callPhpFunction(){
    $.ajaxSetup({
      url: 'ajax.php',
      type: 'POST',
      async: false
    });
    
    var returnedValue = $.ajax().responseText;
    
    alert(returnedValue);
}

The result in image form:

To summarize, I am making an Ajax request via jQuery to a PHP document who's source is a simple hello world output.

The expected result is "Hello". The result I'm getting (via both javascript request and visiting the file directly) is a HTML document. This is happening on every browser, no matter what the echo content is, which makes me think it's PHP or the server doing it, but I really don't have a clue. I've never seen this before

0 Answers
Related