0

Basically what I want to do:

  • Send query to database and get the response in PHP - SUCCESS
  • Put the response into an organised multidimensional array (PHP) - SUCCESS
  • Encode the array with JSON and send it to JavaScript - SUCCESS
  • Parse the data with JSON.parse() and generate HTML - FAILED

What I have tried so far:

  • Tried online JSON parser - worked perfectly (https://codebeautify.org/json-parser-online)
  • Deleted UTF-8 sentence at the beginning of PHP file as mentioned here.
  • Tried to not use JSON - did not work at all - Javascript received string "Array"
  • Debug my code anyway I can with typeof() JSON.stringyfy() etc functions and setting the result inside my HTML to see the result.

I have wasted 8 hours on this and I'm losing my hope.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
</head>
<body>
    <div id="output"></div>

    //Some scripts... 

</body>
</html>

PHP:

<!doctype html>
<html>
<head>
<title>Title</title>
</head> 
<?PHP 

//Got the connection, received data and now putting it to an array

$qry = "SELECT * FROM img LIMIT 0,9";
    $qry_result = mysql_query($qry) or trigger_error(mysql_error(). " in ".$qry);
    while($row = mysql_fetch_array($qry_result, MYSQL_BOTH)){
        //Store values in multidimensional array for returning
        
        $arraytoreturn[] = $row;
    }
    
        //close the connection and return array
        mysql_close($dbhandle); 
        
        echo json_encode($arraytoreturn);
?>
<body>
</body>
</html>

JavaScript(only the function that has something to do with my problem):

function drawOutput(response) {
    "use strict";
    var array = JSON.parse(response);
    var container = document.getElementById('output');
    //Try out the code with first item and "ID" that is for sure in my array
    container.innerHTML = array[0].ID;
}

When I DID NOT use JSON.parse() and on the last line of Javascript wrote container.innerHTML = response; then I got as result my query result from db:

[{"0":"1","ID":"1","1":"569f303b99f31","UID":"569f303b99f31","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"2","ID":"2","1":"569f308b900ed","UID":"569f308b900ed","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"3","ID":"3","1":"569f3112d4111","UID":"569f3112d4111","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"4","ID":"4","1":"569f315428ad7","UID":"569f315428ad7","2":"Horvaatia hommik","HEADING":"Horvaatia hommik","3":null,"DESCRIPTION":null},{"0":"5","ID":"5","1":"569f31943eea2","UID":"569f31943eea2","2":"Riias","HEADING":"Riias","3":null,"DESCRIPTION":null},{"0":"6","ID":"6","1":"569f31ea7f62a","UID":"569f31ea7f62a","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"7","ID":"7","1":"569f3266b5b81","UID":"569f3266b5b81","2":"Riias","HEADING":"Riias","3":null,"DESCRIPTION":null},{"0":"8","ID":"8","1":"569f332d883d1","UID":"569f332d883d1","2":"Horvaatia trip - Poolas","HEADING":"Horvaatia trip - Poolas","3":null,"DESCRIPTION":null},{"0":"9","ID":"9","1":"569f33b357d2f","UID":"569f33b357d2f","2":"Soneburg","HEADING":"Soneburg","3":null,"DESCRIPTION":null}]

Could it be because of the null values in my array? Or what?

user2532781
  • 47
  • 1
  • 5
  • 3
    Why the markup in the PHP script? O.o – Andreas Jan 20 '16 at 16:40
  • 1
    when and where are you calling the `drawOutput` function? – CodeGodie Jan 20 '16 at 16:43
  • Do you want to do something like this? http://stackoverflow.com/questions/34899188/return-response-from-php-to-jquery-using-ajax/34900381#34900381 – Rakhal Imming Jan 20 '16 at 16:44
  • According to your PHP script, you are not showing the full `response` in your question. – frz3993 Jan 20 '16 at 16:45
  • your DB results look wonky. Why are your index keys like that? (`"0", "ID", "1", "UID", ...`) – CodeGodie Jan 20 '16 at 16:54
  • I would suggest, putting a `debugger;` statement in your `drawOutput` and look at the response and work from there... – Remi Smirra Jan 20 '16 at 16:56
  • "I have wasted 8 hours on this and I'm losing my hope." Welcome to programming. While undoubtedly true, that comment is off topic for Stack Overflow. http://cogsci.stackexchange.com/questions/tagged/positive-psychology may help. – Mad Physicist Jan 20 '16 at 17:10
  • why are you using `MYSQL_BOTH` ? do you really need your results like that? – CodeGodie Jan 20 '16 at 17:13
  • Can you show us the code for your AJAX request? I think I might know the problem – Matthew Herbst Jan 20 '16 at 17:18
  • Look at the very first comment above, 3 hours ago. You're echoing all sorts of HTML code, how is `JSON.parse()` supposed to deal with that? – miken32 Jan 20 '16 at 20:27
  • Also, you're using decade old database functions. They won't work in a current version of PHP. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – miken32 Jan 20 '16 at 20:28

0 Answers0