Form adding data twice, Grid only inserts once


#1

We are using a Grid and Form on the same page to take some user input, when we add a new row and enter data in the grid we only see 1 row appear in the DB, when we add the new data in the form and hit Save we get 2 rows added to the DB.
Can anyone suggest why?
Thanks

dhtmlxEvent(window,"load",function(){

function sort_custom(a,b,order){
var n=a.length;
var m=b.length;
if(order=="asc")
    return n>m?1:-1;
else
    return n<m?1:-1;
};	

var layout = new dhtmlXLayoutObject(document.body,"2U");
layout.cells("a").setText("Customer Circuits");
//layout.cells("a").setWidth(1200);
layout.cells("b").setText("Edit Row Data");
layout.cells("b").setWidth(400);

var ClientCircuitGrid = layout.cells("a").attachGrid();	
ClientCircuitGrid.setHeader("Customer,Billing Reference,Supplier,Service Start Date,Contract Start Date,Contract End Date,Contracted Months,Asset Reference,Product Code,Charge Description,Unit Charge To Customer,Monthly or Annual Charge");
ClientCircuitGrid.setColumnIds("Customer,BillingReference,Supplier,ServiceStartDate,ContractStartDate,ContractStartDate2,ContractedMonths,AssetReference,ProductCode,ChargeDescription,UnitChargeToCustomer,MACharge");
ClientCircuitGrid.setInitWidths("250,150,150,150,150,150,150,200,200,200,150,250,*");   //sets the initial widths of columns
ClientCircuitGrid.setColAlign("left,left,left,left,left,left,left,left,left,left,left,left");     //sets the alignment of columns
ClientCircuitGrid.setColTypes("ed,ed,ed,dhxCalendarA,dhxCalendarA,dhxCalendarA,ed,ed,ed,ed,ed,ed");               //sets the types of columns
ClientCircuitGrid.setColSorting("str,str,str,str,str,str,str,str,str,str,str,str");  //sets the sorting types of columns
ClientCircuitGrid.attachHeader("#select_filter,#text_filter,#select_filter,#select_filter,#text_filter,#text_filter,#combo_filter,#text_filter,#text_filter,#text_filter,#text_filter,#select_filter");
ClientCircuitGrid.setDateFormat("%d/%m/%Y", "%Y-%m-%d");
ClientCircuitGrid.attachFooter("Items: {#stat_count},,,,,,,,,,Units: {#stat_total},");
ClientCircuitGrid.init();
ClientCircuitGrid.sortRows(0,"str","asc");
ClientCircuitGrid.load("getData.php");
;

var ClientCircuitForm = layout.cells("b").attachForm();
ClientCircuitForm.loadStruct("formCC.xml");
ClientCircuitForm.bind(ClientCircuitGrid);
ClientCircuitForm.attachEvent("onButtonClick", function(name){
ClientCircuitForm.save();     //sends the values of the updated row to the server
});

var dpg = new dataProcessor("writeData.php");         //inits dataProcessor
dpg.init(ClientCircuitGrid);   //associates the dataProcessor instance with the grid

dpg.attachEvent("onAfterUpdate", function(sid, action, tid, tag){
if (action == "inserted"){
ClientCircuitGrid.selectRowById(tid);        //selects the newly-created row
ClientCircuitForm.setFocusOnFirstActive();//set focus to the 1st form's input
}});

var toolbar = layout.attachToolbar();
toolbar.setIconsPath("icons/");
toolbar.loadStruct("toolbar.xml");
toolbar.attachEvent("onclick",function(id){
if(id=="newContact"){ 
    var rowId=ClientCircuitGrid.uid();
    var pos = ClientCircuitGrid.getRowsNum();
    ClientCircuitGrid.addRow(rowId,["New Customer","",""],pos);
    ClientCircuitGrid.selectRowById(rowId);
    ClientCircuitForm.setFocusOnFirstActive();
	ClientCircuitForm.save();     //sends the values of the updated row to the server
};
if(id=="delContact"){
    var rowId = ClientCircuitGrid.getSelectedRowId();
	var rowIndex = ClientCircuitGrid.getRowIndex(rowId);
	if(rowId!=null){
	ClientCircuitGrid.deleteRow(rowId);
	if(rowIndex!=(ClientCircuitGrid.getRowsNum()-1)){
	ClientCircuitGrid.selectRow(rowIndex+1,true);
	} else{
	ClientCircuitGrid.selectRow(rowIndex-1,true)}}
	}});
 });
</script>

#2

Clicking on the “newContact” button:

if(id=="newContact"){ 

you add the record to the grid:

 ClientCircuitGrid.addRow(rowId,["New Customer","",""],pos);

which triggers the dataprocessor to send data to the database:
as the dataprocessor is assigned to the grid

dpg.init(ClientCircuitGrid);

and then you sen the data to the database from the form.save():

ClientCircuitForm.save();