V. Alen
V. Alen

Reputation: 88

Not getting data in return with AJAX

This is the data I'm trying to get

public function dohvatiZadatak($id){
    $this->id = $id;
    $conn = $this->connect()->prepare("SELECT * FROM zadatci WHERE id=:id");
    $conn->bindParam('id', $this->id);
    $conn->execute();
    $zadatak = $conn->fetch(PDO::FETCH_ASSOC);
    return json_encode(array('zadatak' => $zadatak));
}

Ajax request

function urediZadatak(id){
      $("#uredi-zadatak-modal").modal('show');
      $.ajax({
         type: "GET",
         url: "../zadatci/uredizadatak.php?id="+id,
         data: id,
         async: false,
         success: function(response){
             console.log(response.zadatak);
         }
       });
 }

I'm getting nothing in return. How do I get that data I'm returning with PHP?

Upvotes: 0

Views: 43

Answers (2)

David
David

Reputation: 218827

Based on the comment thread above...

Your function returns a result:

return json_encode(array('zadatak' => $zadatak));

But you're not printing that result to the output:

$zadatak->dohvatiZadatak($id);

All you need to do is print it to the output:

echo $zadatak->dohvatiZadatak($id);

Upvotes: 2

Pedro Henrique
Pedro Henrique

Reputation: 619

In order for the response to be sent back from PHP you need to use echo. On your code you should add the following to the place calling dohvatiZadatak($id):

echo dohvatiZadatak($id);

And than on your front-end you can use:

function urediZadatak(id){
    $("#uredi-zadatak-modal").modal('show');
    $.ajax({
        type: "GET",
        url: "../zadatci/uredizadatak.php?id="+id,
        data: id,
        async: false,
        success: function(response){
            var parsedResponse = JSON.parse(response);
            // use the response as parsedResponse.key
        }
    });
 }

Upvotes: 1

Related Questions