2

I am trying to return ajax response in json, but when I print it in log it gives null even tables has rows, my php code is:

if(isset($_GET['proid'])){
    $projid = $_GET['proid'];
    include(db.php);
    $res = mysqli_query($con, "SELECT * FROM data WHERE project_id LIKE '%$projid%'");      
    while($row = mysqli_fetch_assoc($res)) 
    {
      $dataarray[] = $row;
    }
    echo json_encode($dataarray);
}

ajax :

$.ajax({
        url : 'getRecStudy.php',
        type : 'GET',           
        data : {proid:study},
        success : function(data) {
            $('#tbody').empty();
            $("#tbody").append(data);
            console.log(data);
        }
    });

whats wrong?

Will
  • 24,082
  • 14
  • 97
  • 108
Vishal B
  • 653
  • 2
  • 12
  • 31

2 Answers2

1

I find no issue in your code except varibales. You need to debug the code in php file

if(isset($_GET['proid'])){
    echo $_GET['proid'] . " is proid";
    $projid = $_GET['proid'];
    include(db.php);
    echo "db connected";
    $res = mysqli_query($con, "SELECT * FROM data WHERE project_id LIKE '%$projid%'");      
    echo "result fetched";
    while($row = mysqli_fetch_assoc($res)) 
    {
      $dataarray[] = $row;
      echo "inside while";
    }
    echo json_encode($dataarray);
    print_r($dataarray);
    exit;
}

after all this hit http://yourdomain.com/yourfile.php?proid=correctvalue

You will get the bug.

Kishor Parida
  • 295
  • 1
  • 10
  • can you give me any example link of ajax json example? – Vishal B Jul 11 '16 at 10:42
  • http://stackoverflow.com/questions/8649621/returning-a-json-object-from-php-in-ajax-call – Jagadeesh Jul 11 '16 at 10:45
  • it is still giving null @kishore – Vishal B Jul 11 '16 at 10:52
  • @VishalBorade null values wont print by the procedure i explained above, it would echo string, error or blank provided you hit `http://yourdomain.com/yourfile.php?proid=correctvalue` correct url in browser not through ajax. Before calling ajax make sure your php file works properly through (non-ajax) – Kishor Parida Jul 11 '16 at 10:55
  • It was problem with connection string, when i wrot full connection string it gave me result. i dont knw why... bdw Thanks. – Vishal B Jul 11 '16 at 10:59
  • Glad to know the issue is solved. If somehow my answer helped you then give it a vote. Happy coding. – Kishor Parida Jul 11 '16 at 11:23
0

use parseJSON method in success function like this

var obj = jQuery.parseJSON( data );

alert( obj.name ); // For example name is a key
Dharmendra
  • 127
  • 12
  • even if we dont parse, it would still log some json encoded data not "null" value. Its loggin "null" value so parsing null json won't produce correct parsed value. – Kishor Parida Jul 11 '16 at 10:31