Basic
Basic

Reputation: 26766

Passing JSON or similar object via URL and parsing in PHP

The situation is as follows:

Due to circumstances beyond my control, I'm writing a back-end which exposes data ONLY through JSON.

The UI is pure HTML/jQuery. All queries from the UI result in a JSON object (often quite complex). I need the UI to be able to pass me a complex object which includes multiple lists of Ids and strings.

What's the neatest way to implement this?

If you've done something similar, I'd be interested in your experiences - especially with regards to passing object definitions to the client.

In this instance, imagine the following Data packet...

{
    Id: 1,
    Status: "Processing",
    RecordTypeAIds: (1,2,3)
    RecordTypeBIds: (4,5,6)
    RecordTypeCIds: null
}

What's the neatest way to send this information from jQuery to PHP (bearing in mind, lists may potentially be nested n-deep)

Thanks for any help you can provide.

Upvotes: 2

Views: 4532

Answers (3)

Magicianeer
Magicianeer

Reputation: 2180

Use the (nearly) all JSON solution of Data={... your object ...}. In PHP you need $data = json_decode($_REQUEST['Data']);. jQuery has a similar function for browser-side support.

For HTML forms and regular URLs .. write a server-side adapter that converts the arguments in $_REQUEST into the equivalent $data structure and calls the same code as above. If you name your HTML fields with PHP array notation then this can be as simple as $data = $_REQUEST;

Example in HTML

<input type='hidden' name='Status' value='Processing'>
<input type='hidden' name='RecordTypeAIds[0]' value='1'>
<input type='hidden' name='RecordTypeAIds[1]' value='2'>
<input type='hidden' name='RecordTypeAIds[2]' value='3'>

Appears in $_REQUEST as (a subset of) your example structure.

The trouble with PHP array notation in form elements is ... if you need to populate such a form from JSON then you must write an mildly complex decode function in Javascript to map a JSON structure into the correct field names. There is no good way to avoid the decode function because the DOM form name system is flat where JSON field names are nested, and for JSON array elements, the names (indices) are implicit. Using anything other than PHP array notation in your form element names only makes more work on the server-side.

Upvotes: 2

Andrew Cox
Andrew Cox

Reputation: 91

Just to throw my opinion in there. I prefer JSON, just send the complex objects via your preferred method(GET/POST) and they almost instantly become an object/array in PHP using globals($_POST['key']) and json_decode(). If your using jQuery the 'sending' portion is easily achieved using $.ajax(), $.get(), or $.post(). POST theoretically has no datalimit so for arbitrarily large data sets it would be preferable.

Upvotes: 3

Nick ODell
Nick ODell

Reputation: 25210

If you want to pass a JSON object to PHP, then the required method is included by default in 5.2.0 and beyond. http://www.php.net/manual/en/function.json-decode.php

Upvotes: 1

Related Questions