question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Form data not submitting to external API

See original GitHub issue

I’m passing next js form parameters to an external API written in Laravel which send a simple email, the API working fine when I send a post request using postman but , when I submit below Next Js form to the API nothing happen, no call make to API please help me

import React, {Component} from 'react'
import fetch from 'isomorphic-unfetch'

class Contact extends Component {
    constructor(props) {
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this)
    }

    handleSubmit = async () => {
        var email = this.refs.email.value.trim();
        var message = this.refs.message.value.trim();

        const res = await fetch('http://iotdash.dev/contact', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: JSON.stringify({
                'email': email,
                'message': message
            })
        });
        const data = await res.json();
    };

    render () {
        return (
            <div className="row contact-wrapper container-width center-align">
                <h3>Contact Us</h3>
                <p className="gray-text">Leave us some information and we will be in touch as soon as possible!</p>
                <form onSubmit={this.handleSubmit}>
                    <div className="col m6 form-field left-align">
                        <label className="gray-text">
                            First Name *
                        </label>
                        <input className="gray-text" ref="first_name" required type="text"/>
                    </div>
                    <div className="col m6 form-field left-align">
                        <label className="gray-text">
                            Last Name *
                        </label>
                        <input className="gray-text" ref="last_name" required type="text"/>
                    </div>
                    <div className="col m6 form-field left-align">
                        <label className="gray-text">
                            Email *
                        </label>
                        <input className="gray-text validate" ref="email" required type="email"/>
                    </div>
                    <div className="col m6 form-field left-align">
                        <label className="gray-text">
                            Phone Number
                        </label>
                        <input className="gray-text" ref="phone" type="text"/>
                    </div>
                    <div className="col m12 form-field left-align">
                        <label className="gray-text">
                            Message *
                        </label>
                        <textarea className="gray-text" ref="message" required/>
                    </div>
                    <div className="col m12 left-align contact-button-wrapper">
                        <input className="button button-yellow submit-button" type="submit" value="Contact Us" />
                    </div>
                </form>
            </div>
        )
    }
}

export default Contact

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
timneutkenscommented, Jul 1, 2017

Found it 😄 The default event was not prevented, which causes a page refresh.

handleSubmit = async (event) => {
        event.preventDefault()
0reactions
timneutkenscommented, Jul 1, 2017

Great 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

AJAX How to submit different form data to external API
You can submit whatever data you want regardless of number of forms using Ajax. Below Ajax does the same var formData = "name=ravi&age=31"; ......
Read more >
Sending form data - Learn web development | MDN
This article looks at what happens when a user submits a form — where does the data go, and how do we handle...
Read more >
Submit Contact Form 7 Field Data to External API or Email
In this video, learn how to pick submitted data from Contact Form 7 and email it or send it to an external API...
Read more >
POST Form Data With JavaScript Fetch API - YouTube
Use JavaScript to collect & submit form data... Professional JavaScript Course: https://bytegrad.com/professional-javascript?n=697  ...
Read more >
Send form data to external applications - Progress Community
Are there any resources discussing how I can send form data to an external application that is not Marketo, Oracle or Insight?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found