PDA

View Full Version : Name arrays and json form load



oldroy
29 Nov 2007, 4:14 PM
Looked high and low for any posts using name arrays in forms. I've got some forms that I want to post with info as arrays. No trouble posting. The trouble is doing a form load with json to populate the fields.

Outgoing:

<input name="agent[firstname]">
<input name="agent[lastname]">

After php json encoding to a file I've been returning this as:
..{data:{agent:{"firstname":"value","lastname","value"}}..

No dice on the form load unless I use name="firstname" and return the json as:
..{data:{"firstname":"value"}}..

Is the native json load for a form just not aware of arrays that are returned?

Here is the code

items: [{
fieldLabel: 'First Name',
name: 'first',
width:190
}, {
fieldLabel: 'Last Name',
name: 'last',
width:190
}, {
fieldLabel: 'Company',
name: 'company',
width:190
}, {
fieldLabel: 'Email',
name: 'email',
vtype:'email',
width:190
}
This sent through a submit to a php page that uses json encode and save to file, and then returned with this:

// simple button add
fs.addButton('Load', function(){
fs.form.load({url:'http://localhost/h2o/ext/forms/mynewdataarray.php', waitMsg:'Loading'});
});

Yields this as a response in firebug and in loads into the form - no problems:

{ success:true, data:{"first":"Roy","last":"Hacker","company":"Interwest","email":"[email protected]"}}
However when I switch to names as part of arrays and post:

items: [{
fieldLabel: 'First Name',
name: 'agent[first]',
width:190
}, {
fieldLabel: 'Last Name',
name: 'agent[last]',
width:190
}, {
fieldLabel: 'Company',
name: 'agent[company]',
width:190,
}, {
fieldLabel: 'Email',
name: 'agent[email]',
vtype:'email',
width:190
}
encodes and ruturns as this:
{ success:true,
data:{"agent":{"first":"Roy","last":"Hacker","company":"Interwest","email":"[email protected]"}}
}
and is loaded with same load as above.

The head scratcher is whether the json encoding is just not correct from php.json_encode() or is the native json data load not aware of name arrays, or not aware of json arrays?

I'm using the name arrays because it makes it much easier to write out xfdf and xml from a post, but also save the json as a data-document that can be recalled later.

Any ideas would be appreciated.

ArtBuilders
29 Nov 2007, 8:14 PM
DIsclaimer: I'm fairly new to the EXTJS framework/JSON. Can you passively return a JSON array and have the fields intuitively extract their expected data? (or am I missing something in your code? ) In terms of returned arrays, in the past I've used loops, something like... (example uses JSON and Prototype)


var response = transport.responseText.evalJSON();
for(var i in response) {
var value = response[i];
if(value){
$(i).value = value; //if not Prototype, then switch to long form
}
}

I'm sure there are better ways to do it. I haven't tested it but this should by adaptable. I'd be interested in what the more seasoned EXTJS/JSON users' prescribed approach is. I'm about to tackle this same issue in the next couple days.

oldroy
29 Nov 2007, 8:30 PM
Hello,
I expect that is what will be needed. The loop will need to be a nested loop as there will be more than one array. I'll look for some loop examples in the forum posts, probably something like a tree load would be similar.

ArtBuilders
29 Nov 2007, 9:37 PM
encodes and ruturns as this:
{ success:true,
data:{"agent":{"first":"Roy","last":"Hacker","company":"Interwest","email":"[email protected]"}}
}

Sorry... My bad. I should have read more carefully. Doesn't look like your JSON is formatted correctly. Should be:


{success:true, data: [{"first":"Georgie", "Last":"Smith"}]}

oldroy
30 Nov 2007, 6:54 AM
I have actually tried all possible combinations for the json.
...{success:true, data: [{"first":"Georgie", "Last":"Smith"}]}...
...{success:true, data:{agent:[{"first":"Georgie"}, {"Last":"Smith"}]}
...{success:true, data:{agent:[{"first":"Georgie", "Last":"Smith"}]}
...{success:true, data: [{"first":"Georgie"},{ "Last":"Smith"}]}...

All Fail....

hendricd
30 Nov 2007, 7:57 AM
or is the native json data load not aware of name arrays, or not aware of json arrays?

Ultimately, (unless you override) you must follow the conventions supported by BasicForm.setValues method. It supports a basic (object) hash or an array of objects. The API docs make it pretty clear.

oldroy
30 Nov 2007, 8:36 AM
I see that in the docs. Thanks. I'll try renaming the post variables and processing them on the other side. So much for JSON as a replacement for anything other than snippets of xml in this case.

oldroy
30 Nov 2007, 10:09 PM
Just built a recursive array transfer/builder to process the post array from form names like agent_first, agent_last into agent[first] and agent[last] and pass the values through as an array to my xmlwriters. Makes xml and xfdf (for pdf form filling) much easier to write.

I've wanted to be able to fill a form, save to json and recall it later as an intermediate dat source, and save the same form variables to an xfdf file builder as an array as needed.

Interesting???