Encapsulation of random amounts - is it possible?

Asked

Viewed 34 times

1

I’m trying to get php to send a response to the Avascript file with information, in which Avascript makes a request for php in ajax, and php sends a random amount of information, php will pull all records from the database and respond to ajax with this information, and finally Avascript will mount html, I at first thought of building html in php, but I saw that this is not a good option.

OBS: there are two PHP files one that will generate the database and the other that will receive the html by 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 Java?

  • 1

    All that you flaunt with PHP, being by echo, print, or any other means, will be defined as the body of the HTTP response returned to the client (in this case, the JS).

  • I forgot to write that they are two distinct files, it was bad =/

  • As well as two distinct files?

  • There are three files, two PHP and a JS, one is the Index.php that will receive the post code $("#id"). html("RESP") and the other PHP that will generate the database.

1 answer

1


To return the PHP data you can use the function echo. It will return what is passed as 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 variable $resposta when the echo.

  • The problem is not sending by echo, but how to generate random amounts to send by echo json_encode($var) //Var || array || object

  • 1

    Sorry, I don’t understand the question, what random amounts do you want to generate? You say generate a random number in the database?

  • This, for example, if the bank has 125 items it will pull 125 items, what defines the amount of information is the amount of the bank.

  • But that while loop will only close when the number of bank replies runs out. If you have 1 response line it runs 1 time, if you have 125 it runs 125 times.

  • But I need to pull the entire database to return the information by json_encode()

  • 1

    The problem then is to determine how many lines will be passed at a time? How to make data pagination?

  • Yes I thought about doing that, I thought well after I had done the post, but I wanted to believe I had a way to do with just one pagination.

  • Suddenly this question helps you in this case https://answall.com/questions/26303/como-fazer-pagina%C3%A7%C3%a3o-php-e-mysql#26400

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.