2

Hello I'm kinda new to programming and I'm trying to use ajax and would like to get the value from this php, but can't get it to work

$userID = $_SESSION['id'];

$query = "SELECT * from ipcr where userID = '".$userID."'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $current_id = $row['id'];
    $current_details = $row['details'];
    $current_dateCreated = $row['dateCreated'];
    $current_ipcrCode = $row['ipcrCode'];
    $current_employeeNumber = $row['employeeNumber'];

    $array = array(
        'id'=>$current_id,
        'details' => $current_details,
        'dateCreated' => $current_dateCreated,
        'ipcrCode' => $current_ipcrCode,
        'employeeNumber' => $current_employeeNumber
    );

    echo json_encode($array);
}

but i keep getting an error:

SyntaxError: Unexpected token < in JSON at position 173

and when i try to validate my json it gives this error

This is the actual output that the php echoed.

{"id":"21836","details":"Details here","dateCreated":"2018-08-01 14:25:28","ipcrCode":"22703","employeeNumber":"140010663"}
{"id":"21837","details":"details here","dateCreated":"2018-08-01 14:25:57","ipcrCode":"22703","employeeNumber":"140010663"}

Is there something wrong with the way I use json_encode? it seems that the format I echoed is wrong.

also this is how my script looks like

function get_ipcr() {
    var userID = <?php echo $_SESSION['id']; ?>;

    $.ajax({
        type: "POST",
        url: "../includes/php/load_ipcr.php",

        dataType: "json",
        success: function(results) {
            alert("success");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }

    });

}
Sciryl
  • 55
  • 1
  • 8
  • Your `echo json_encode($array);` is *inside* your whole loop so it is echoing multiple times. Move it after the loop – Sean Aug 28 '18 at 23:05

2 Answers2

4

The issue is you are echoing the JSON object in loop. This results in something like {valid JSON}{valid JSON} which isn't a valid JSON.

You can fix this by:

$employees = array();
while ($row = mysqli_fetch_assoc($result)) {
    $current_id = $row['id'];
    $current_details = $row['details'];
    $current_dateCreated = $row['dateCreated'];
    $current_ipcrCode = $row['ipcrCode'];
    $current_employeeNumber = $row['employeeNumber'];
// Record into employees array.
    $employees[] = array(
        'id'=>$current_id,
        'details' => $current_details,
        'dateCreated' => $current_dateCreated,
        'ipcrCode' => $current_ipcrCode,
        'employeeNumber' => $current_employeeNumber
    );
}
echo json_encode($employees);
Ketan Yekale
  • 2,108
  • 3
  • 26
  • 33
1

Welcome to stackoverflow!

There's several things you are going to hear about from different comments / answers, I recommend that you listen to them - it may seem overwhelming, or like "too much info" - but trust me, we are all here to help!

The answer to your problem
Bake the entire JSON string into a single array (an array of arrays), rather than echoing out the JSON string multiple times within the loop.

Additional tip
Also, as a tip, you are making more typing for yourself by assigning all the variable ($current_id = $row['id'], etc). You can just use the $row['id'] directly ... below you can see that I've shown one way to streamline your code:

// shorthand version of $array = array()
$array = [];
while ($row = mysqli_fetch_assoc($result)) {
    // don't bother assigning $row values to a series of variables
    // removed all the lines like the next one....
    // $current_id = $row['id'];
    // push this record onto the array of arrays....
    $array[] = [
        // assign the array values directly from $row
        'id'             =>$row['id'],
        'details'        => $row['details'],
        'dateCreated'    => $row['dateCreated'],
        'ipcrCode'       => $row['ipcrCode'],
        'employeeNumber' => $row['employeeNumber']
    ];
}

echo json_encode($array);

Lastly, and MOST IMPORTANT
Please, please - do NOT write query statements like that. It is wide open to SQL Injection attacks, which are a serious issue.

Please see this post about how to prevent SQL injection attacks.

random_user_name
  • 25,694
  • 7
  • 76
  • 115