Difference Between PHP Echo and Return in Terms of a Jquery Ajax Call

Difference between php echo and return in terms of a jQuery ajax call

Well, the ajax call reads the response from the server, and that response must be rendered as some type of readable data, such as application/json or text/html.

In order to write that data, you need to echo it from the server with PHP.

The return statement doesn't write data, it simply returns at the server level.

What is the difference between print_r and echo when providing results to an AJAX call?

The most common built-in output functions in PHP are: echo, print, printf, print_r, var_dump, and var_export. They are all designed to output data, albeit, in slightly different ways. Which is why there are specific use cases associated with each function.

  • echo: The most notorious. It's what you should be using to output data to an AJAX caller. It's simple and effective. It's actually a language construct, so it doesn't require you to use brackets. You just write echo 'string'; or echo $something;, etc.

    It also supports multiple comma-delimited values, making it handy in a variety of use cases; e.g., echo 'string1', 'string2', 'string3';. Note that echo returns null (nothing).


  • print: Also a language construct, it's nearly the same as echo, with the difference being that it accepts only one argument and always returns the same thing: 1. It's OK to use print instead of echo if you like, but many popular PHP projects have code standards that ask you not to do so. So print has become, more or less, an unused (or rarely used) function in PHP.

  • printf: This function and its brother sprintf() are designed to output formatted strings. It's slightly more complex, but does have specific use cases. For example, these are often used to output an integer as a binary number or octal number, for translation, among many other options that are well documented at PHP.net — that's a separate topic.

  • print_r, var_dump, and var_export: These are most commonly used when debugging code or testing code in one way or another. You will rarely find a use case for them in production code. That said, these allow you to pass in complex data types, such as an entire array, and print_r recursively, to dump the entire array for analysis. var_dump does pretty much the same thing, but uses a slightly different approach that can sometimes be useful. var_export has the advantage of outputting a parseable string.

    print_r and var_export both support a parameter that can 'return' the value it would normally output, which can be a handy feature. Something that var_dump is not capable of.

Echo'ing php as return value to ajax call

Two things: first, you shouldn't need to use flush() - simply echo your data and end your script.

Second, the results of your echo statement: data: ["insert your data here"] is not valid JSON and you're not going to be able to decode it on the client.

When should I return true/false to AJAX and when should I echo true/false

You might also look at returning HTTP error codes rather than returning a "success" response (HTTP status code 200) when the request wasn't really successful, and then use an error callback to handle unsuccessful requests.

But if you want to keep using status code 200 (and a lot of people do that):

The data transferred between the client and the server is always text. The trick is to make sure that the client and server agree on how the client should deserialize the text (transform it upon receipt). Typically you might return one of four things:

  1. HTML (if it's going to populate page elements)

  2. JSON (if you want a lightweight, fast way to send data to the client)

  3. XML (if you want a heavier-weight, fast way to send data to the client)

  4. Plain text (for whatever you want, really)

What the client does will depend on what Content-Type header you use in your PHP page.

My guess is that you're using any of several content types that end up passing on the data as a string to your callback. The string "true" is truthy, but so is the string "false" (only blank strings are falsey).

Long story short: I'd probably use this in my PHP:

header('Content-Type', 'application/json');

...and the return this text from it:

{"success": true}

or

{"success": false}

...and then in your success handler:

if (response.success) {
// It was true
}
else {
// It was false
}

Alternately, you can return a Content-Type of text/plain and use

if (response === "true") {
// It was true
}
else {
// It was false
}

...but that's kind of hand-deserializing where you could get the infrastructure to do it for you.

Ajax PHP Jquery - echo-ing back data

use $_REQUEST it will handle both the GET and POST

$item1 = $_REQUEST['test'];

by default ajax request is GET type, either specify expilicitly the type like

$.ajax(
{
url: 'test.php',
type:'POST'
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})

or use $_GET like

item1 = $_GET['test'];

echo $item1;

how to write url in ajax request in codeigniter php?

You are confuse between the meaning of [Return, Echo] in PHP,

Echo

echo — Output one or more strings

Return

return returns program control to the calling module. Execution
resumes at the expression following the called module's invocation.

and as long as the Ajax response callback is reading a server response [output], you must send an output to the server.

public function ajax_load()
{
echo "Hello";
}

Further reading :-

  • What is the difference between PHP echo and PHP return in plain English?

  • Difference between php echo and return in terms of a jQuery ajax call

  • a short and simple answer



Related Topics



Leave a reply



Submit