Hundotte Hundotte - 1 year ago 100
jQuery Question

Why is AJAX data not received by PHP?

I have the following extremely simple PHP tester:

<script src=""></script>

<button id="button">send request</button>

type: "POST",
url: "ajaxTest.php",
data: {userresponse: "hi"},
success: function(data){

var analyse = function () {
$variable = $_POST["userresponse"];
case "hi":
echo 'alert("' . $variable . '")';
echo 'alert("LOGIC")';

What's supposed to happen is that when I click the button, it sends the data
userresponse: "hi"
to the server, and then PHP receives it and alerts the value (i.e. "hi")

However, despite the fact that the file paths are correct, the AJAX send is OK in XHR, the PHP does not receive the value of the data, and the
returns the entire HTML document.

What is going on and how do I fix this?

Answer Source

Remove analyze() and put your php code in external file called ajaxTest.php, your code works perfect just remove your php code fron analyze and request for external this is bad practice having both in same file(header problems).

Proof: enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download