Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyForm extends React.Component {
- constructor() {
- super();
- this.state = {};
- this.handleSubmit = this.handleSubmit.bind(this);
- }
- handleSubmit(event) {
- event.preventDefault();
- const data = new FormData(event.target);
- this.setState({
- res: stringifyFormData(data),
- });
- // fetch('/api/form-submit-url', {
- // method: 'POST',
- // body: data,
- // });
- }
- render() {
- return (
- <div>
- <form onSubmit={this.handleSubmit}>
- <label htmlFor="username">Enter username</label>
- <input id="username" name="username" type="text" />
- <label htmlFor="email">Enter your email</label>
- <input id="email" name="email" type="email" />
- <label htmlFor="birthdGate">Enter your birth date</label>
- <input id="birthdate" name="birthdate" type="text" />
- <button>Send data!</button>
- </form>
- {this.state.res && (
- <div className="res-block">
- <h3>Data to be sent:</h3>
- <pre>FormData {this.state.res}</pre>
- </div>
- )}
- </div>
- );
- }
- }
- ReactDOM.render(<MyForm />, document.getElementById('app'));
- function stringifyFormData(fd) {
- const data = {};
- for (let key of fd.keys()) {
- data[key] = fd.get(key);
- }
- return JSON.stringify(data, null, 2);
- }
Add Comment
Please, Sign In to add comment