PDA

View Full Version : Sending JsonStore to PHP



d4h0nk
20 May 2011, 3:15 AM
Hi everyone,

I've a problem. I want to send a Store (what means: the DataRecords) to a php-script. The JS sends just some objects to PHP and I can't deal with it. I ran totally out of coffee and my brain refuses to work anymore.
Can anyone help?

Idea: get the modified records in the store "myAdressStore" and send them via POST to a php-script.

Here is the JS-coding:



sT.on('click', function() {
var params = "store="+myAdressStore.getModifiedRecords();
var request = new XMLHttpRequest();

function setTask() {
if (window.XMLHttpRequestHead) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
}
}
if (!request) {
Ext.Msg.alert(Error', '');
return false;
}
else {
request.open('POST', 'sendtask.php', true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(params);
request.onreadystatechange = interpretTask;
}
}
function interpretTask() {
switch (request.readyState) {
case 4:
if (request.status != 200) {
Ext.Msg.alert('Error', '');
} else {
var content = request.responseText;
//content = eval("(" + content + ")");
//content = eval('(' + content + ')');
if (!content){
Ext.Msg.alert('Error', '');
}
else
{
for (var i=0; i < content.length; i++){
var dataarray = content[i];
if ( dataarray.rc == '1' ) {
Ext.Msg.alert('Ok', 'Success!');
}
}
}
}
break;
default:
break;
}
}
setTask();

}, this);





Firebug says, I'm sending something like
store=[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
in POST-params. But how to deal with it in PHP? How can I get the records?
I tried to

json_decode($_POST["store"])
but it doesn't work.

Any hints are very welcome!

Thx in advance!

drian
21 May 2011, 1:52 PM
The logic goes like this:

Loop through your modified records, put the records data that you need (this case - record_id and record_name) inside an array and send it through ajax to the server. On the server you'll need to decode your POST param.




var arr = [];
Ext.each(store.getModifiedRecords(), function(record){
arr.push({id: record.get('record_id'), name: record.get('record_name')});
});

Ext.Ajax.request({
url: 'your url here',
params: { my_param: Ext.encode(arr)},
success: function(response){
// do your success code here
},
failure: function(response){
// do your error code here
}
});

d4h0nk
21 May 2011, 11:26 PM
Hi drian,

thanks a lot for your post!
In my current solution, I'm looping through the modified records and sending an XHR for every record. This is quite unperformant.

I think your solution is much smarter. I'll give it a try!

Thx a lot!

d4h0nk

d4h0nk
23 May 2011, 6:53 AM
Thx a lot! It works great! You made my day!

mschwartz
23 May 2011, 7:21 AM
Look at Ext.Ajax.request() as well.