Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

As the title suggests I'm trying to access a nested javascript object property from within PHP.

My object is declared like so.

var requestParams = {
        apiKey: appData.apiKey,
        action: "addJob",
        name : name,
        note: note,
        dropoff: dropoffAddress,
        noOfCars: noOfCars,
        carType: carType,
        pickUpDate: pickUpDate,
        pickUpTime: pickUpTime,
        pickUpDateTime: {}
    };

The pickUpDateTime(last property of the requestParams object) property has a nested object which I add key value pairs dynamically using a for loop.

for(var x = 0; x < dateArray.length; x++){
        var dbMoment = moment(dateArray[x] + " " + pickUpTime,appData.displayDateFormat);
        requestParams.pickUpDateTime[x] = dbMoment.format(appData.dbDateFormat);
    }

When I log the object this is what I see in the console. So far so good.

{"apiKey":"ba6f4fc3-0f52-11e3-a51d-fefdb24fa1e7","action":"addJob","name":"Dave Manning","note":"","dropoff":"51st street","noOfCars":"1","carType":"saloon","pickUpDate":"11/06/14,13/06/14,15/06/14","pickUpTime":"14:10",******"**pickUpDateTime":{"0":"2014-06-11 14:10:00","1":"2014-06-13 14:10:00"**,**"2":"2014-06-15 14:10:00"}****,"phone":"452345657654745","pickupAddress":"popes quay","pickupLat":51.9013,"pickupLng":-8.47616,"noOfDates":3}

However, after I send it to my PHP script I can't seem to access the pickUpDateTime object. At the moment I am trying to access it using the code below. It won't work though. Moreover, when I include this line of code the function won't return anything so I can check the contents of $pickUpDateTime.

$pickUpDateTime = json_decode($this->getRequestValue("pickUpDateTime", true));

I can access every other property. So for instance this works fine.

$noOfDates = $this->getRequestValue("noOfDates", true);

I know it's probably a relatively easy fix but I can't seem to get it working.

Any help is much appreciated.

Here's what I get when I executed var_dump($_REQUEST)

responseText: " ↵Array↵(↵    [apiKey] => ba6f4fc3-0f52-11e3-a51d-fefdb24fa1e7↵    [action] => addJob↵    [name] => dave↵    [note] => ↵    [dropoff] => blarney street↵    [noOfCars] => 1↵    [carType] => saloon↵    [pickUpDate] => 11/06/14,13/06/14,15/06/14↵    [pickUpTime] => 15:43↵    [pickUpDateTime] => Array↵        (↵            [0] => 2014-06-11 15:43:00↵            [1] => 2014-06-13 15:43:00↵            [2] => 2014-06-15 15:43:00↵        )↵↵    [phone] => 5643563456435↵    [pickupAddress] => popes↵    [pickupLat] => 51.89397↵    [pickupLng] => -8.47685↵    [noOfDates] => 3↵    [_] => 1402497974065↵)↵[]"
share|improve this question
    
var_dump($this->getRequestValue("pickUpDateTime", true)); what do you get? –  user574632 Jun 11 at 13:33
    
What does the getRequestValue method do? It is probably something that needs to be changed in there. –  Kevin Choppin Jun 11 at 13:34
    
It does the same thing as $variable = $_POST["variable"] –  Mike Jun 11 at 13:36
    
If you've already decoded the entire string the pickupDateTime will already have been decoded. No need to decode it again - just pick up the values and work with them. –  Mike W Jun 11 at 13:36
    
I've taken out the json decode, still not working. –  Mike Jun 11 at 13:45

3 Answers 3

I think that "pickUpDateTime" is nested inside a JSON string, although I don't know exactly what the getRequestValue method does, I'm not sure if it will pull pickUpDateTime or the other params from this string.

You will first need to pass the entire string into json_decode and assign that to a variable:

$jsonValue = json_decode($requestParams);
$pickUpDateTime = $jsonValue['pickUpDateTime'];
share|improve this answer
    
I've already done that. –  Mike Jun 11 at 13:46

You need to send that data as a json string using JSON.stringify:

var data = [];

for(var x = 0; x < dateArray.length; x++){
        var dbMoment = moment(dateArray[x] + " " + pickUpTime,appData.displayDateFormat);
        data[x] = dbMoment.format(appData.dbDateFormat);
    }

var jsonString = JSON.stringify(data);

var requestParams = {
        apiKey: appData.apiKey,
        action: "addJob",
        name : name,
        note: note,
        dropoff: dropoffAddress,
        noOfCars: noOfCars,
        carType: carType,
        pickUpDate: pickUpDate,
        pickUpTime: pickUpTime,
        pickUpDateTime: jsonString
    };

//php
$pickUpDateTime = json_decode($this->getRequestValue("pickUpDateTime", true));
share|improve this answer
    
Tried that. Still didn't work. –  Mike Jun 11 at 13:58
    
Please elaborate on "didnt work". What does the request object look like in the backend? Does it contain a property called pickUpDateTime at all? Are you getting and js errors, or is the problem entirely in the backend? What does the actuall http request look like (check with fiddler, firebug, chrome dev tools or similar) –  user574632 Jun 11 at 14:07
    
When I done a console log I got pickUpDateTime":"[\"2014-06-11 14:56:00\",\"2014-06-13 14:56:00\"]" –  Mike Jun 11 at 14:07
    
Ok so its fine on the from end then, whats the request look like in the backend? –  user574632 Jun 11 at 14:22
    
I've posted the $_REQUEST dump above. When I do a var_dump after the decoding the json I get a null. –  Mike Jun 11 at 16:57

If the getRequestValue method is just getting POST values, then you're not posting JSON. You're sending to the server like a form post.

Declare pickUpDateTime as an array in JS instead of an object;

var requestParams = {
        apiKey: appData.apiKey,
        action: "addJob",
        name : name,
        note: note,
        dropoff: dropoffAddress,
        noOfCars: noOfCars,
        carType: carType,
        pickUpDate: pickUpDate,
        pickUpTime: pickUpTime,
        pickUpDateTime: []
    };

If your JS library is clever enough it will post pickUpDateTime as an array. If not your best solution would be to post as JSON data to PHP and decode it server side into an object.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.