The behavior that is triggered when you click the "x" depends on which button you have set as the close button. The button that is currently selected has the text "(close)" at the end.
First: create a microflow that closes the page and handles the data. Then add this microflow as a button on the control bar. Open the properties of the button and set "Is close button" to true. If you don't want the button to be visible then you can add the following css in the style box of the button: display:none;
https://world.mendix.com/display/refguide6/Is+close+button+Property