Instead of getting the selected nodes in the onSelect event of the dynatree object, you can get it by obtaining a reference to the tree.
From the dynaTree documentation:
var tree = $("#tree").dynatree("getTree");
After that you should be able to do:
var selKeys = $.map(tree.getSelectedNodes(), ..................
You can do all of that within your form's register button's click function.
Perhaps with something like:
$("#form_pdetail").on("click", "#register_button", function() {
var detail_add = $("#form_pdetail").serialize();
var tree = $("#tree").dynatree("getTree");
var selKeys = $.map(tree.getSelectedNodes(), function(node) {
return node.data.key;
}
detail_add.selectedItems = selKeys.join(", ");
var request = $.ajax({
type: 'POST',
url: "{{ path('product_detail_create') }}",
data: detail_add,
success: function() {
request.abort();
loadCenterLayout("{{ path('product_detail_list') }}");
}
});
});