Passing Arrays to PHP via AJAX
April 4th, 2010 Posted in Developing with EXTJS, JQuery Snippets, JavaScript Snippets
I've written on processing AJAX queries before but neglected discussing how to pass an array to PHP for processing. This is an example of getting all checked rows in an EXTJS grid and posting them to an awaiting PHP script.
First we have the users confirm they really want to process all checked records:
Ext.MessageBox.confirm('Confirm', 'Are you sure your want to email login info to all selected users?', confirmEmailUsers);
Now the jQuery method:
function confirmEmailUsers(btn){ if(btn=='yes'){ var recArray=new Array(); var ids=new Array(); var recArray = mailqueueGrid.getSelectionModel().getSelections(); for (var i = 0 ; i < recArray.length ;i++) { ids[i] = recArray[i].get('id'); } $.ajax({ type: "POST", dataType: 'json', url: '/cpsia/?c=form&m=processqueue', data: 'ids='+ids, success: function(msg){ alert(msg) } }); } }
Finally, the PHP script to processes it:
$ids = explode(",",$_POST['ids']); foreach ($ids as $id) { echo $id .''; }