PDA

View Full Version : Processing Ext Json data in PHP - How?



TheRebelGriz
27 May 2007, 2:36 PM
Here is what I am working with:
1. Ext - 1.0.1a
2. PHP - 4.4.5
3. JSON.php = 1.31

Working from Scott Walter's Screencast about EditorGrid as my base code, I am at the point where I want to 'Save Changes' by sending them to a php script on the server.. then save the changes to MySQL...

However, i have run into a snag and that it appears that the processing part of the JSON data on the php side doesn't seem to be working and/ot the POST of data that is made is being skewed somehow....

I've tried all both Ex.util.JSON.encode & YAHOO.ext.util.JASON.encode... but without success... I even but together a control php script to make sure things were good on the php side and everythings worked in the control script but not in my data.php even with the same code from the control script....

The only thing different is that the data is recieved via the $_POST versus a hard coded string... I know this has to be simple and I have seem to hit a road block and not sure were to go next...

Below are the code snipets that I am working with ....

Can anyone please steer me in the right direction and/or provide a complete working example??? Any assistance provided is greatly appreciated!!!! Thanks!!!

JavaScript Code:

function saveChanges() {
console.info("saveChanges: " );
jsonData = '[';
for ( i=0; i<dsAttrs.getCount(); i++ ) {
record = dsAttrs.getAt(i);
if ( record.data.newRecord || record.dirty ) {
jsonData += Ext.util.JSON.encode(record.data) + ',';
//jsonData += 'YAHOO.ext.util.JSON.encode(record.data) + ',';
}
}
jsonData = jsonData.substring(0,jsonData.length-1) + ']';
console.log("jsonData: " + jsonData);

new Ext.data.Connection().request(
{ url: dataUrl
, method: 'post'
, scope: this
, waitMsg: 'Saving Changes, please wait...'
, params: { html: 'dmlData', data: 'attrs', jsondata: jsonData }
, callback: function(options, bSuccess, response) {
var rs = Ext.decode(response.responseText);
console.log("response:"+response);
if( 'object' != typeof rs ) {
Ext.MessageBox.alert('Response Error', 'The Server returned an invalid object.', null);
}
if ( true === rs.success ) {
Ext.MessageBox.alert('Success', 'The request processed.', this);
} else {
Ext.MessageBox.alert('Failure', 'The request was not processed.', this);
}
}
}
);
}

data.php Code - This the code the Javascript calls:

require_once('../includes/php/JSON.php');
$json = new Services_JSON;
$j2data = $_POST['jsondata'];
$data = $json->decode($j2data);
$cnt = 1;
foreach ( $data as $rec ) {
echo "<br><br>rec.$cnt::";
$cntCol = 1;
foreach ( $rec as $col ) {
echo "<br>&nbsp;&nbsp;col.col.$cntCol::" . $col;
$cntCol++;
}
$cnt++;
}


data.control.php Code:

require_once('../includes/php/JSON.php');
$json = new Services_JSON;

//$j2data = "[{\"attr_id\":\"-1\",\"attr_type\":\"DIVISIONS\",\"attr_domain\":\"DIVISION\",\"attr_name\":\"CENTRAL\",\"status\":\"Y\",\"description\":\"Central Division\",\"use_values_yn\":\"N\",\"data_type\":null,\"data_length\":null,\"data_precision\":\"0\",\"data_mask\":null,\"created_by\":\"user@somwhere.com\",\"created_date\":\"2007-05-26 10:31:16\",\"modified_by\":null,\"modified_date\":null}]";
$j2data = '[{"attr_id":"-1","attr_type":"DIVISIONS","attr_domain":"DIVISION","attr_name":"CENTRAL","status":"Y","description":"Central Division","use_values_yn":"N","data_type":null,"data_length":null,"data_precision":"0","data_mask":null,"created_by":"user@somwhere.com","created_date":"2007-05-26 10:31:16","modified_by":null,"modified_date":null}]';

$data = $json->decode($j2data);
$cnt = 1;
foreach ( $data as $rec ) {
echo "<br><br>rec.$cnt::";
$cntCol = 1;
foreach ( $rec as $col ) {
echo "<br>&nbsp;&nbsp;col.col.$cntCol::" . $col;
$cntCol++;
}
$cnt++;
}

jsakalos
28 May 2007, 2:52 AM
Hi,

first of all I would check if the json string you create before you send it to server is valid. It's easy just after you create the string:




var testJson = Ext.decode(jsonData);
debugger;

If the string is valid your problem will be server-side. Any chance to install PHP 5.2.0 or later? I have never had problems with json_decode and json_encode functions of this version of PHP.

I you have to stick with PHP 4 then you can look here: http://pecl.php.net/package/json or you can search these forums; I think there was something on PHP4/JSON.

Hi,

TheRebelGriz
28 May 2007, 9:26 AM
Well, called the hosting provider and l'm in luck there are running in dual mode right now....

Change my extsions to php5 and now I'm running under PHP 5.1.4 and everything is working as it should....

Thanks for the assistance...