1

I have two JavaScript classes:

function Sensor(sensorNumber, name, m, b, selected) {
    "use strict";
    this.sensorNumber = sensorNumber;
    this.name = name;
    this.m = m;
    this.b = b;
    this.selected = selected;
}

The Chart class below contains an Array of the Sensor class above, some variables, and a timeSpan class:

function Chart(index, allsensors) {
    "use strict";
    this.chartName = $("#newChartName").val();
    this.chartNumber = index;
    this.sensorsArray = allsensors;
    this.time = new TimeSpan();
}

I send the "chart" object variable to PHP via Jquery AJAX:

function obtainChartData(chart) {
    "use strict";
    $.ajax({
        url: 'server/ExtractTelemetry.php',
        type: "POST",
        data: JSON.stringify(chart),
        success: function (msg) {
            alert(msg);
        }
    });
}

PHP Receives it, and this is where I'm stuck. I need to obtain the "this.Selected" for each element in the sensor object array.

<?php
    $json = file_get_contents('php://input');
    $chart = json_decode($json);
    echo $startTime = $chart->time->startSec." "; //CORRECT
    echo $endTime = $chart->time->endSec." "; //CORRECT
    echo $chartName = $chart->chartName." "; //CORRECT
    echo $chartNumber = $chart->chartNumber." "; //CORRECT

    for(...) {
    echo $allSensors = $chart->allSensors[someIndexValue]->selected; //why does this not work??
    }
?>

UPDATE:

Here is the PHP Error output

enter image description here

JSON output(shortened but should be enough info):

object(stdClass)#1 (4) {  
["chartName"]=>  string(7) "Chart#1"  
["chartNumber"]=>  int(1)
["sensorsArray"]=>  object(stdClass)#2 (44) {    
    ["B0"]=>    object(stdClass)#3 (5) {      
        ["sensorNumber"]=>      int(0)      
        ["name"]=>      string(10) "SRBUS(mV)"      
        ["m"]=>      string(6) "1.2811"      
        ["b"]=>      string(8) "-4.1559"      
        ["selected"]=>      bool(false)    
        }    
    ["B1"]=>    object(stdClass)#4 (5) {
        ["sensorNumber"]=>      int(1)      
        ["name"]=>      string(10) "SRBUS(mA)"      
        ["m"]=>      string(6) "0.6137"      
        ["b"]=>... 
9
  • 1
    Shouldn't that be $chart->sensorsArray[someIndexValue]->selected? Commented Sep 22, 2015 at 8:34
  • @Phylogenesis, you are correct... I fixed it, but that leads to another error. I updated the image Commented Sep 22, 2015 at 8:40
  • You'll have a lot more luck using var_dump($chart); and looking at its actual structure. Commented Sep 22, 2015 at 8:41
  • Show please the json string passed to the server. Commented Sep 22, 2015 at 8:48
  • I added it to my post Commented Sep 22, 2015 at 8:53

1 Answer 1

3

json_decode returns an stdObject. Properties of that object can be access by

$myObject = json_decode($myString);

$myObject->myProperty;

If you want an array returned from json_decode pass a secnond parameter 'true'

$myArray = json_decode($myString, true);
Sign up to request clarification or add additional context in comments.

2 Comments

That leads to this error: <br /><b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\CSUNSat1-TelemetryWebApp\CSUNSat1Telemetry\server\ExtractTelemetry.php</b> on line <b>6</b><br /><br /><b>Notice</b>: Trying to get property of non-object
the chart itself is an array then also: this should work: $startTime = $chart['time']['startSec'];

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.