Skip to content

Save Local Data Array Back to Server via Ajax

Below is the code snippet to submit local array back to server through Ajax POST. jQuery Form plugin is required. Users must supply their own server-side save routine URL e.g. "save_local_array.php". The edit.php provided with phpGrid only works with database data source. 

    <script src="http://malsup.github.com/jquery.form.js"></script>
    <form id="admin_form">
        <div>
            <input type="submit" value="Submit Local Changes">
        </div>
    </form>

    <script>
        $(function() {
            // bind to the form's submit event
            $('#admin_form').submit(function(event) {
                $(this).ajaxSubmit({
                    type: 'post',
                    dataType:'json',
                    url:'save_local_array.php',
                    data:{
                        langArray:[] //leave as empty array here
                    },
                    beforeSubmit: function(arr, $form, options){
                        options.langArray = $('#data1').jqGrid('getRowData'); //current
                        console.log(JSON.stringify(options.langArray));
                        // return false; // here to prevent submit
                    },
                    success: function(){
                        // add routine here when success
                    }
                });

                // return false to prevent normal browser submit and page navigation
                return false;
            });
        });
    </script>

The data format posted back to the server is JSON format. 

Visit demo.phpcontrols.com and select "Local Array" demo, enable browser console, and the hit "Submit local changes" button, you should see a sample of the JSON data similar to the below posted back to the server.  

[{"id":"1","foo":"6ffcc0d3641930e3d8980ec43343ccc5","bar1":"bar1","bar2":"bar1","cost":"5","name":"Decker","quantity":"78","discontinued":"1","email":"grid_99@example.com","notes":""},{"id":"2","foo":"23e846638607adcbc730817a77581220","bar1":"bar2","bar2":"bar2","cost":"59","name":"Kocoboo","quantity":"90","discontinued":"1","email":"grid_1@example.com","notes":""},{"id":"3","foo":"f75dddd1e79826a219cb0bec217dc096","bar1":"bar3","bar2":"bar3","cost":"54","name":"Kocoboo","quantity":"45","discontinued":"0","email":"grid_33@example.com","notes":""}]



Feedback and Knowledge Base