Ajax Date Return Behavior

2

My doubt is technical.

I would like to understand better what happened. I spent a few days trying to solve a problem in my code. I found that the functions of ajax or php were wrong. Two PHP functions were very similar, but one functioned and the other did not. Both should return a $resposta variable, with an HTML code to insert into the page. Ex:

$resposta = '<span>Alô mundo!</span>';

In the end, I noticed that the one that worked had the lines of code:

echo $resposta;
return $resposta;

and the one that did not work had only

return $resposta

It turned out that the 'echo' command was what was really working. So, my question is: if the function in ajax said:

function chamaM(id_chamado){ //-------> Estava funcionando
    $.ajax({
        type: 'POST',
        url: 'scriptPHP3.php',
        data: 'id=' + id_chamado,
        success: function(data){
            $('#teste').html(data);
        }
   });
}

function chamaTb(id_chamado, id_m){ //-------> NÃO estava funcionando
    $.ajax({
        type: 'POST',
        url: 'scriptPHP3.php',
        data: { id: id_chamado, ind_m: id_m },
        success: function(data){
            $('#teste').html(data);
        }
   });
}

Why is 'success' returning echo and not 'return'? I ask why I just put the echo to see if the function was working. Otherwise, I would be trying to understand the error so far. Thank you very much for the answer. If you need, here is the relevant code in HTML and PHP:

.HTML

    <div id="menu-container">
        <a id="m1" class="ativo">m1</a>
        <a id="m2">m2</a>
        <a id="m3">m3</a>
    </div><!-- #menu-container -->

    <div id="teste">/*DIV ONDE SERÁ POSTO O CONTEÚDO PROCESSADO*/</div>


.PHP

if (isset( $_POST["ind_m"] ) && isset( $_POST["id"] ) ) {
    cliqueTb( $_POST["id"], $_POST["ind_m"] );
} elseif (isset ($_POST["id"] ) ) {
    cliqueM( $_POST["id"] );
 }

function cliqueTb( x, y ) { //-------> NÃO estava funcionando
    PROCESSAMENTO;
    return $resposta;
}

function cliqueM( x ) { //-------> Estava funcionando
    PROCESSAMENTO;
    echo $resposta;     //-------> 'echo' retorna o valor e não o 'return'
    return $resposta;
}
    
asked by anonymous 25.07.2014 / 22:28

2 answers

3

Your technical question has a very, very simple answer.

AJAX is a technique for requesting content from a "backstage" address. This content is the output that the server sent to the browser in response.

And how does an application send content? Echoing / "printing" something in the program. When the request is completed, that content will be shown to whoever requested it, be it a person or a programmatic routine.

return does not produce content , it deflects the program's execution flow from the local scope of a function for global / external:

$name = 'Bruno Augusto';

$age = getAge();

function getAge() {
    return 26;
}

echo 'Nome: ', $name, ' .Idade: ', $age; // Nome; bruno Augusto. Idade: 26

While defining the variables, we are in global / external scope. Hence we have a PHP function that has its own, local, independent scope. And then we have echo , back to the global scope, which shows the phrase.

Without the return code execution would enter the function when it was invoked ( $ age ), but would never leave it.

And if return does not produce content, there will not be a Response Body . And if you do not have a body, you do not have what AJAX works with.

    
25.07.2014 / 22:41
1

Your JavaScript / jQuery code makes an HTTP request to the server. PHP receives, handles, and responds to this request. Both languages (JavaScript and PHP) do not talk directly, only via HTTP requests and responses.

In the case of HTML, an HTTP response is basically text, containing your HTML. The echo of PHP command writes to the output of the program, which is what goes into the HTTP response. The return does not write to output, it only causes the function to return a value to the caller (in PHP itself). And the call that did not work was in this line:

cliqueTb( $_POST["id"], $_POST["ind_m"] );

The line does nothing with the function return. It could write the value returned in the program output, which would be another way to make the content appear in the HTTP response:

echo cliqueTb( $_POST["id"], $_POST["ind_m"] );
    
25.07.2014 / 22:41