Porting data submission logic from php to JS