I've figured out the reason this button wasn't working sometimes -- there is invalid HTML nesting here. Note above that you open a table, open a form, close the table, then close the form. This is invalid nesting, so the browser assumes that you wanted to close the form immediately before closing the table, and does that. Then your submit button technically exists outside of the form, which causes the issue.