0

I'm trying to send an array of data from PHP to ajax. I'm using echo json_encode to do it. When I do that, I try 'console.log(data)' to see the response data but it not show anything. How can I get it to display the data? I really don't know what I'm missing here. I have this script:

var scard = $('#cardid').val();
$.ajax({
    type: 'GET',
    url: 'cardapi.php?scard=' + scard,
    success: function (data) {
        console.log($.parseJSON(data));
        console.log(data);
    }
});

And here is my code for cardapi.php

if(isset($_GET["scard"])){
    $scard = $_GET["scard"];
    $data = array();

    $sql = "SELECT * FROM training_record WHERE cardref_no='$scard'";
    $q = sqlsrv_query($conn, $sql);

    while($rw = sqlsrv_fetch_array($q, SQLSRV_FETCH_ASSOC)){
        array_push($data,[
            "employee_no" => $rw["employee_no"],
            "dept_id" => $rw["dept_id"],
            "name_th" => $rw["name_th"],
            "surname_th" => $rw["surname_th"],
            "signed_status" => 1,
        ]);

    }

    echo json_encode($data);
}

So I try to follow this echo json_encode() not working via ajax call

It still not show anything. Please tell me why?

Thank you.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
Meawmill
  • 67
  • 1
  • 13

1 Answers1

0

You may try the following:

  • Always check the result from the sqlsrv_query() execution.
  • Always try to use parameterized statements. Function sqlsrv_query() does both statement preparation and statement execution, and can be used to execute parameterized queries.
  • Check the result from the json_encode() call.
  • Fix the typing errors ("signed_status" => 1, should be "signed_status" => 1 for example).

Sample script, based on your code:

<?php
if (isset($_GET["scard"])) {
    $scard = $_GET["scard"];
    $data = array();

    $sql = "SELECT * FROM training_record WHERE cardref_no = ?";
    $params = array($scard);
    $q = sqlsrv_query($conn, $sql, $params);
    if ($q === false) {
        echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
        exit;
    }   

    while ($rw = sqlsrv_fetch_array($q, SQLSRV_FETCH_ASSOC)) {
        $data[] = array(
            "employee_no" => $rw["employee_no"],
            "dept_id" => $rw["dept_id"],
            "name_th" => $rw["name_th"],
            "surname_th" => $rw["surname_th"],
            "signed_status" => 1
        );
    }

    $json = json_encode($data);
    if ($json === false) {
        echo json_last_error_msg();
        exit;
    }   

    echo $json;
}
?>
Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • Oh Thank you, I already know what happened. Error is "Malformed UTF-8 characters, possibly incorrectly encoded" What should I do next? – Meawmill Mar 09 '20 at 10:03
  • 1
    @Meawmill You may try to ignore the invalid characters using `json_encode($data, JSON_INVALID_UTF8_IGNORE)`, but probably reading `[UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) is a good starting point. – Zhorov Mar 09 '20 at 10:08