Andrew Barker Andrew Barker - 7 months ago 32
PHP Question

delete records from store with checkbox selection

I have this ajax request that iterates through a store and deletes all selected records.

code

Ext.Ajax.request({
url: 'system/index.php',
method: 'POST',
params: {
class: 'LicenseFeatures',
method: 'delete',
data: Ext.encode({
feature_id: ( function(){
var e = "";
var sel = Ext.getCmp('featureGrid').getSelection();
var c = 0;
for( var i in sel ) {
var x = ( c == 0 ) ? e = sel[i].data.feature_id : e += "," + sel[i].data.feature_id;
c++;
}
return e;
})()
})

},
success: function( response ){
Ext.MessageBox.alert( 'Status', 'Record(s) has been deleted.' );
Ext.getStore('LicenseFeaturesStore').reload();
},
failure: function(){
Ext.MessageBox.alert( 'Status', 'Failed to delete records.' );
}
});


Currently the code retrieves 1 id from the grid and deletes it. What I need to do is get two Id's from the grid as I need to run a specific sql to the database. The sql needs two inputs, here is the sql

public function delete( $vars ){
$sql = "DELETE FROM `LicenseFeatures` WHERE feature_id in({$vars->data->feature_id}) AND license_id in({$vars->data->license_id})";
if( $result = $vars->db->query( $sql ) ) {
echo json_encode( array( "success" => true,"sql"=>$sql ) );
} else {
echo json_encode( array( "success" => false ) );
}
}

Answer

Try and change the data property to this.

data: Ext.encode(( function(){
    var feature_id,
        licence_id;
    var sel = Ext.getCmp('featureGrid').getSelection();
    var c = 0;
    for( var i in sel ) {
        if (c == 0) {
            feature_id = sel[i].data.feature_id;
            licence_id = sel[i].data.licence_id;
        } else {
            feature_id += "," + sel[i].data.feature_id;
            licence_id += "," + sel[i].data.licence_id;
        }
        c++;
    }
    return {
        feature_id: feature_id,
        licence_id: licence_id
    };
})())