Encapsulation of random amounts - is it possible?

1

I'm trying to get php to send a response to the informational javaScript file, in which javaScript places a request for php in ajax, and php sends amount of information in random amount, php pulls from the database all records and will answer the ajax with this information, and finally javaScript will mount the html, I at first thought of mounting the html in php, but I saw that this is not a good option.

NOTE: There will be two PHP files that will generate the database and another that will receive the html through the ajax response.

My codes are just below.

javascrip

$.ajax({
url : "file.php",
type : "POST",
data : { alpha : null},
success : function(){console.log("work")},
error : function(){console.log("error")}
});

PHP

$q = "SELECT * FROM 'table'";
$r = $conn->query($q);

while ($row = $r->fetch_assoc()) {

}

echo json_encode($var); //Var || array || objeto

My question, how do I send this information back to javaScript?

    
asked by anonymous 25.06.2018 / 16:37

1 answer

1

To return the PHP data you can use the echo function. It will return whatever is passed as a parameter.

$q = "SELECT * FROM 'table'";
$r = $conn->query($q);
$resposta ='';
while ($row = $r->fetch_assoc()) {
    // Aqui você ajusta a resposta como quiser.
    $resposta .= $row;
}
echo $resposta;

JavaScript will then receive what is in the $resposta variable when given echo .

    
25.06.2018 / 20:13