1

I want php file to return data (from the database) on ajax call. Ajax call returns an error alert everytime. I tried everything, but have no idea how to return array from PHP to ajax call

So far, I made this..

ajax call

function dohvatiPrveTriAkcije(id){
var url = 'http://localhost/ljekarna/model/akcija.php';
$.ajax({
    type: "POST",
    url: url,
    cache: false,
    data: { "kategorija": id},
    dataType: "json",
    success: function (data) {
        document.getElementById("kat_id").innerHTML += 'aaa';
    },
    error: function () {
        alert('Pojavila se greška pri dohvacanju akcija za odabranu kategoriju');
    }
});
return null;
}

php class

<?php

 require_once 'baza_model.php';

 $akcija = new Akcija();

if (isset($_GET['kategorija'])) {
echo $_GET['kategorija']; 
$akcije = $akcija->dohvatiPrveTriAkcijeZaKategoriju($_GET['kategorija']);
echo $akcije;
}

class Akcija{
private $baza;   

static function dohvatiPrveTriAkcijeZaKategoriju($kategorija){

    $baza = new Baza();
    $akcije = array();

    $upit = 'SELECT lijek.naziv, akcija.postotak, akcija.datum_zavrsetka FROM akcija join lijek on akcija.lijek = lijek.id 
            join kategorija on lijek.kategorija = kategorija.id 
            where akcija.datum_zavrsetka > CURDATE() AND kategorija.id = ' . $kategorija . ' AND akcija.status = 1 
            ORDER BY akcija.datum_zavrsetka ASC LIMIT 3';

    $rez = $baza->selectDB($upit);
    while($red = $rez->fetch_assoc()){
        echo "id: " . $red["id"];
        $akcije[] = $red;
    }
    return $akcije;
}

}

I also tried this...

enter image description here

2
  • Try commenting out this line dataType: "json", in the ajax like //dataType: "json", then in the success and in the error write console.log(data); immediately after : function(data) {. See what the php returns in your console. You need the error: to return data as well. It may help determine what the php page is doing. Also, you should just check the console to see there are no errors coming back in the javascript. Commented Jun 2, 2016 at 20:49
  • After commenting out line dataType: "json" the ajax call results in success function. But then I tried this in success function and it's not working, I guess that data is still empty for(elem in data){ document.getElementById("kat_id").innerHTML += 'b'; } Commented Jun 2, 2016 at 21:27

1 Answer 1

1

You need a json formatted string returned by the server. Use json_encode() instead of trying to echo out your array (which is what's giving you your array to string error).

Sign up to request clarification or add additional context in comments.

2 Comments

Tnx, but ajax results with error again. I need to get variable $akcije to ajax, and then print all the data in success function
@Fale1994 have you removed echo $_GET['kategorija']; from your php code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.