-1

Finally i get some output out of PHP instead of just the word "array". This took me a day, AAAAHHHH.

(see here for the solution)

So this is the return value from a XMLHTTP request (to PHP), via a callback in JS. I got it with print_r in PHP.

I post a snippet of the results here. My new question:

  • what is this data structure made of?
  • how to get elements out of this structure, such as CourseID (in JS)?

    [0] => Parse\ParseObject Object
    (
        [serverData:protected] => Array
            (
                [CourseID] => DEMO2
                [EndDate] => DateTime Object
                    (
                        [date] => 2017-03-31 10:26:00.000000
                        [timezone_type] => 2
                        [timezone] => Z
                    )
    
                [InfoTitle1] => Welcome
                [InfoText1] => Welcome to your course, called "sense & sales". 
                [Admin] => Remco@Demo1
            )
    

My PHP code is

function getGroups(){
  $query = new ParseQuery("CourseInfo");
 $query->equalTo("Admin", "Remco@Demo1");
 $results = $query->find();


// echo $results      // this lead to the string "array"
//print_r($results);  // this leads to the complicated array
//echo "<script>\r\n var phpOutput = " . json_encode($results) . ";\r\n console.log(phpOutput);\r\n</script>";
// this leads to [{},{}]; 
}
Community
  • 1
  • 1
Remzo
  • 103
  • 6

3 Answers3

1

What you want is a JavaScript Object, so you need to use JSON.

(JSON means JavaScript Object Notation.)

PHP has a json_encode function, which will do the work for you: (http://php.net/manual/en/function.json-encode.php)

json_encode — Returns the JSON representation of a value

Try this:

PHP:

echo "<script>\r\n var phpOutput = " . json_encode($output) . ";\r\n console.log(phpOutput);\r\n</script>";

Press F12 in your browser to open the browser's Console log window in Developer Tools to see the new JavaScript Object.

Sean Kendle
  • 3,538
  • 1
  • 27
  • 34
  • thanks Sean! i changed $ouput and then it outputs this: – Remzo Jan 31 '17 at 20:57
  • Check on the variable name you're `json_encode`ing. Is it `$output`? If not, change that variable to what it's supposed to be. This is why it's helpful to include the code in question. – Sean Kendle Jan 31 '17 at 20:58
  • Please update your question with the PHP code so we can see what you might be doing wrong. – Sean Kendle Jan 31 '17 at 21:00
0

You may want to convert your PHP object to JSON first via json_encode().

This will allow you to easily work with it in JavaScript like you normally would with any JavaScript object.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

I stopped trying to work with this array/object in JS. But instead i managed it on the PHP side and sent the results to JS.

Now it is all simple. (-;

Remzo
  • 103
  • 6