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;
}