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.

5
  • Have you tried to echo an example array with example values instead of your generated? Commented Mar 9, 2020 at 9:14
  • 2
    console.log(data)); is a syntax error. Commented Mar 9, 2020 at 9:16
  • Remove the last bracket from console.log(data)); so you get console.log(data); Commented Mar 9, 2020 at 9:23
  • @u_mulder Sorry, I typed wrong in here. Error didn't occur there. Commented Mar 9, 2020 at 9:37
  • Yes, I tried this " echo json_encode(["hi" => "test", "hi2"=>"test2"]); ". It show. I don't understand. :( @RobinGillitzer Commented Mar 9, 2020 at 9:44

1 Answer 1

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;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Oh Thank you, I already know what happened. Error is "Malformed UTF-8 characters, possibly incorrectly encoded" What should I do next?
@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 is a good starting point.

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.