I'm trying to echo $_POST
with $.ajax
in PHP with no success. In Xdebug I see the $_POST
get the right value and executing the echo $_POST
line, but I keep getting the else
output clause. Also in chrome I see the headers that are sent valid. All the code is in the same page index.php.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.post demo</title>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
</head>
<body>
<button type="button" id="but">Click Me!</button>
<script>
$('#but').click(function() {
$.ajax({
type: "POST",
url: "index.php",
data: {name: "John"},
success: function() {
alert('success');
}
});
});
</script>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
echo $_POST['name'];
} else {
echo "Nothing to Show";
}
?>