0

I want to create a simple form using react. So I though I go with the onSubmit function of the form element, but as it turns out I don't get the form data. data of my handleSubmit function is empty. Why is that?

There is a fiddle that works: https://jsfiddle.net/everdimension/5ry2wdaa/ I don't see the difference.

export default function App() {
  function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    const data = new FormData(e.target);
    console.log('do the submitting', data)
  }

  return (
    <div className="App">
      <form onSubmit={handleSubmit}>
        <label>
          Media Plan
          <input type="text" name="media-plan" placeholder="Media Plan" required />
        </label>

        <fieldset>
          <legend>Time span selection</legend>
          <label>
            Start date
            <input type="date" name="start-date" required />
          </label>

          <label>
            End date
            <input type="date" name="end-date" required />
          </label>
        </fieldset>

        <fieldset id="submit">
          <legend>Send options</legend>
          <button type="submit" name="copy">
            Copy plan
          </button>
          <button type="submit" name="save">
            Save plan
          </button>
        </fieldset>
      </form>
    </div>
  )
}
pgalle
  • 216
  • 3
  • 13

1 Answers1

0

I had to read them this way, the formData object only appears empty.

const formData = new FormData(e.target)

for (var value of formData.values()) {
  console.log(value);
}
pgalle
  • 216
  • 3
  • 13