Results 1 to 5 of 5

Thread: Sending JsonStore to PHP

  1. #1
    Sencha User
    Join Date
    May 2011
    Posts
    7

    Default Sending JsonStore to PHP

    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:

    Code:
    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
    Code:
    json_decode($_POST["store"])
    but it doesn't work.

    Any hints are very welcome!

    Thx in advance!

  2. #2
    Sencha User
    Join Date
    Dec 2010
    Posts
    139

    Default

    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.


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

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

  3. #3
    Sencha User
    Join Date
    May 2011
    Posts
    7

    Default

    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

  4. #4
    Sencha User
    Join Date
    May 2011
    Posts
    7

    Default

    Thx a lot! It works great! You made my day!

  5. #5
    Sencha User mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,061

    Default

    Look at Ext.Ajax.request() as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •