Search code examples
javascriptreactjsformssemantic-ui

React Semantic UI Form - Two submit buttons


I'm pretty new at ReactJs, I'm trying to submit a form, but one of the form values must depend on which submit button triggers the submit

I've coded something like this

<Form onSubmit={(e) => {
        e.preventDefault();
        const {target} = e;
        console.log('FormData', Object.fromEntries(new FormData(target)));
      }}>
... form fields ...
<Button icon="thumbs up" type="submit" content="Authorize" name="status" value="A" positive />
<Button icon="thumbs down" type="submit" content="Reject" name="status" value="R" negative />
</Form>

The log shows all form values correctly but the "status" field. Is there any way to achieve the expected behaviour? Any alternative is very welcome

Thank you


Solution

  • You can get the status value from the submit button on the event.submitter property and append this to the FormData.

    See this answer for more info:

    https://stackoverflow.com/a/64857928/681058