waitingtocompile waitingtocompile - 3 months ago 4
PHP Question

PHP AJAX success value empty

I've been having trouble with AJAX requests giving blank responses. Minimal version of the problem:

test.html:

<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function request() {
$.ajax({
url: 'test.php',
type: 'post',
data: {'info': 'thing'},
success: function(response)
{
alert(response);
}
});
}
</script>
<button onclick="request();">TEST</button>
</body>
</html>


test.php


$info = 'nope';
if(isset($_POST['info'])) $info = $_POST['info'];

return $info;


When I press the button the alert box is simply blank.

Answer

test.php doesn't output anything. Replace this:

return $info;

with this:

echo $info;
Comments