Get response from PHP file using AJAX

Get response from PHP file using AJAX

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Capturing Server Responses in jQuery AJAX

When using jQuery to submit data via $.ajax(), a common stumbling block is understanding where the response from the server actually lives. In your specific case, you want process.php to echo out a value like "apple" or "plum", and you need that string stored in a variable on the client side so you can use it later.

The core issue in your provided snippet is that the success callback is currently empty. You are invoking alert('return sent'), which executes immediately, before the asynchronous request even completes. To access the data sent back from process.php, you must reference the response object within the function arguments passed to the success handler.

The Success Callback and Response Handling

In jQuery's $.ajax() method, the success parameter is a callback function that receives at least one argument: the server's response data. This data corresponds exactly to what was echoed or returned by your PHP script.

Here is how you should modify your JavaScript to capture that value:

function returnwasset() {
    $.ajax({
        type: "POST",
        url: "process.php",
        data: somedata, // Ensure 'somedata' is defined before this function call
        success: function(response) {
            console.log('Server sent:', response);
            
            // Now 'response' holds the string "apple" or "plum"
            var fruit = response; 
            
            alert(fruit);
        }
    });
}

Notice the syntax correction here: success is a property that expects a function definition (function(response) { ... }), not just success function(){}. The variable name inside the callback, often named response, data, or result, contains the raw output from your server. You can assign this directly to a new variable like var fruit = response;.

How process.php Should Output Data

Regarding your question about whether you need to echo JSON or if plain text is fine: for simple string responses, plain text is perfectly acceptable. If process.php outputs just echo "apple";, the JavaScript variable will contain the string "apple".

However, there are two critical requirements for this to work reliably:

  1. Output must be a single block: Ensure process.php does not output any HTML before or after the text (like <html> tags or whitespace). The response should start immediately with the data.
  2. Correct Data Structure: If your PHP logic determines the value based on input, ensure that conditional logic outputs exactly one line per request. For example:
<?php
// process.php
if ($input === 'red') {
    echo "apple";
} else if ($input === 'purple') {
    echo "plum";
}
?>

If process.php returns nothing (e.g., due to a logic error or missing data), your JavaScript variable will be an empty string, which might cause issues if you try to parse it as JSON later.

Handling POST Data and Naming the Request

You asked how to name the POST request in the code. In jQuery's $.ajax() configuration, there is no specific property called "name" for the entire request object. Instead, you send data using the data option. This can be a simple string, an array, or a JavaScript object.

If you need to send form fields, you typically serialize the DOM element or pass a key-value object:

var somedata = {
    fruit_type: 'apple',
    quantity: 5
};

When process.php receives this via $_POST, the keys will be fruit_type and quantity. If you are sending a raw string instead of an object, simply pass the string directly in the data property. The server must know which key to expect based on how you structure the client-side data.

Potential Pitfalls

A frequent mistake is expecting the response inside the alert() call immediately after defining the function. AJAX is asynchronous; the browser executes line-by-line, but waits for the network request in the background before resolving the success callback. If you try to use the variable outside the success function without a promise or async/await wrapper, it will likely be undefined.

If your application grows beyond simple string echoes, switching to JSON responses and parsing them with JSON.parse() is a best practice for scalability and error handling. However, for the immediate need of echoing "apple" or "plum", plain text works fine provided no extra characters are emitted.

Our network