Netlify-forms Contact Form submission 404 error - reactjs

I am having trouble submitting my Contact Page through using Netlify-Forms, I am getting a 404 error.
I have the following form :-
<form name="Portfolio_Contact"
method="POST"
data-netlify="true"
data-netlify-honeypot="bot-field"
action="/thank-you">
<div className="form-group">
<input
type="text"
name="name"
placeholder="name"
className="form-control"
/>
<input
type="email"
placeholder="email"
name="email"
className="form-control"
/>
<textarea
name="message"
rows="5"
placeholder="message"
className="form-control"
></textarea>
<div data-netlify-recaptcha="true"></div>
</div>
<button type="submit" className="submit-btn btn">
send me your message
</button>
</form>
And when I submit it, I am first getting a 404 error, and then getting my thank-you page as expected.
Am I doing something wrong? I can see my “Portfolio_Contact” in the Forms section.
Thanks for your help and time.

You have a reCAPTCHA set but your form is not expecting it since is not defined in your <form> tag. Just add:
<form name="Portfolio_Contact"
method="POST"
data-netlify="true"
data-netlify-honeypot="bot-field"
data-netlify-recaptcha="true"
action="/thank-you">
Spot the data-netlify-recaptcha="true". You can find more information in Netlify documentation for reCAPTCHA 2.
I am getting this in the network :- Referrer Policy: strict-origin-when-cross-origin
Besides of this configuration on the <form> tag, you need to set up your POST action configuration. Netlify is kind of weird in their responses, a 404 doesn't mean that your form doesn't exist, it means that the request failed. I usually apply the exact <form> configuration that I've posted but I add a custom submit handle function which sets the request configurations:
<form name="Portfolio_Contact"
method="POST"
data-netlify="true"
data-netlify-honeypot="bot-field"
data-netlify-recaptcha="true"
action="/thank-you">
<div className="form-group">
<input
type="text"
name="name"
placeholder="name"
className="form-control"
/>
<input
type="email"
placeholder="email"
name="email"
className="form-control"
/>
<textarea
name="message"
rows="5"
placeholder="message"
className="form-control"
></textarea>
<div data-netlify-recaptcha="true"></div>
</div>
<button type="submit" className="submit-btn btn" onClick={handleSubmit}>
send me your message
</button>
</form>
Your handleSubmit function:
const handleSubmit = event => {
event.preventDefault();
// do your verifications and checks
if(!verified) return false
const REQUEST_PARAMETERS = {
method: `POST`,
headers: { 'Content-Type': `application/x-www-form-urlencoded` },
body: encode({ ...data }), //your data here. Needs to have your form-name attribute set
};
fetch(`/`, REQUEST_PARAMETERS)
.then(() => {
console.log(`OK`);
})
.catch(error => alert(error));
};
Note: your request won't work in your local environment
This configuration is important because sets the headers of the request, what is failing in your sample.
You can check for a sample in this DEV article.

Related

Trouble getting two separate forms, (on different pages), to work on Netlify with Gatsby

The Netlify site name is: https://southsidepaint-prototype.netlify.app
I am using React with Gatsby and Sass, all up to date with the latest versions.
I have a simple contact form on the contact page (’/contact’) that looks like this:
<form name="contact-form" method="POST" data-netlify="true" >
<input type="hidden" name="form-name" value="contact-form" />
<div className={ctSty.formGroup}>
<input name="name" type="text" placeholder="Name" required/>
</div>
<div className={ctSty.formGroup}>
<input name="email" type="email" placeholder="Email" required />
</div>
<div className={ctSty.formGroup}>
<input name="phone" type="tel" placeholder="Phone Number" required/>
</div>
<div className={ctSty.formGroup}>
<textarea name="message" placeholder="Write your message"></textarea>
</div>
<button type='submit' value="Submit">Send message</button>
</form>
Now this form above is submitting correctly, didn’t work with a honeypot or recaptcha but that’s ok. The problem is when I click to submit my other form which exists on a separate page (’/careers’), I receive no 404 errors, no warnings, nothing happens. I check the deploy log for warnings too, only had one warning: “warning undefined”; I’m not sure where this particular warning comes from but it existed before the forms were added, so I think that’s ok too. Netlify is able to see that I have a second form, I have the folders on my forms page but nothing is being submitted in spam or verified. I have scoured through the docs, videos and articles but cannot pin down a fix.
Things I have tried:
I have even reduced my second form down to three simple inputs, name, email and phone number and still the same result.
I have changed the name attribute in my form multiple times, cleared the cache on deploy.
changed the name from “form-name” in my
<input type="hidden" name="form-name" value="applicationsV2" />
Added an “onSubmit” to the form
<form name="applicationV2" method="POST" data-netlify="true" onSubmit="submit">
The second form’s code is below:
<form name="applicationV2" method="POST" data-netlify="true" onSubmit="submit">
<input type="hidden" name="form-name" value="applicationsV2" />
{/* FULL NAME */}
<div className={crSty.formGroup}>
<label for="name" required>
<span><CgAsterisk /></span> Full Name <br/>
<input name="Full_name" type="text" placeholder="e.g. Phillip Anthropy" required/>
</label>
</div>
{/* EMAIL ADRRESS */}
<div className={crSty.formGroup}>
<label for="email" required>
<span><CgAsterisk /></span> Email Address <br/>
<input name="Contact_email" type="email" placeholder="e.g. user#email.com" required />
</label>
</div>
{/* PHONE NUMBER */}
<div className={crSty.formGroup}>
<label for="phone" required>
<span><CgAsterisk /></span> Phone Number <br/>
<input name="Contact_phone" type="tel" placeholder="e.g. 555-0000" required/>
</label>
</div>
</form>
<div style={{textAlign:'center'}}>
<button type='submit' value="submit">SubmitApplication</button>
</div>
You have a typo in your second form (applicationV2). While in the form name appears applicationV2, in the value of the form-name field is applicationsV2. It should be:
<form name="applicationV2" method="POST" data-netlify="true" onSubmit="submit">
<input type="hidden" name="form-name" value="applicationV2" />
Keep in mind that with your previous snippet your form was in a kind of limbo because there wasn't a match between forms names so, the data was sent but never caught by any Netlify form. Fixing the typo should fix your issue too.
For further logs, check the Network tab in the inspector tools to see whats Netlify returning in the form response.

Login credentials showing in url parameters in react js

I am a beginner in React JS, I created a login form and after click on login button then username and password is showing in the URL. I don't want to show this in URL. How to solve this?
Here is my code:
<form className="loginform" onSubmit={this.ValidateUser}>
<input
className="form-control"
type="text"
placeholder="Email"
name="email"
autocomplete="off"
value={this.state.email}
onChange={(e) => this.changeEmailHandler(e.target.value)}
/>
<input
className="form-control"
type="password"
placeholder="Password"
name="password"
value={this.state.password}
onChange={(e) => this.changePasswordHandler(e.target.value)}
/>
<input
type="submit"
id="kt_login_signin_submit"
className="btn btn-brand"
value="Sign In"
/>
</form>
Adding e.preventDefault() in ValidateUser method should do the trick.
Edit-01:
already pointed out in the comments.

Netlify hides the email field

I recently created a Netlify application, with Gatsby JS and a Netlify form. I have the following as my form field:
<form
action="#"
method="post"
name="contact"
className="footer-form"
data-netlify="true">
<input
id="name"
type="text"
name="name"
placeholder="Name"
value={this.state.name}
className="footer-form__input"
onChange={e => this.setState({ name: e.target.value })}
/>
<input
id="email"
name="name"
type="email"
className="footer-form__input"
value={this.state.email}
onChange={e => this.setState({ email: e.target.value })}
placeholder="Email address"
/>
<textarea
id="message"
name="message"
className="footer-form__input footer-form__textarea"
value={this.state.message}
onChange={e => this.setState({ message: e.target.value })}
placeholder="Your message"
/>
<button type="submit" className="footer-form__button">
Submit
</button>
</form>
In development, the email field shows as planned. But in production, specifically on the netlify system, only the name and the message field shows.
Form submission works fine in production by the way, showing only the "name" and the "message" inside the Netlify dashboard.
Could there be a reason why this is happening?
The first 2 of your inputs have the same name:
<input
id="name"
type="text"
name="name"
...
/>
<input
id="email"
name="name"
type="email"
...
/>
Netlify may rely on the field name to differ between different inputs. Would you try fixing that and see if the problem still occurs?

Upload picture in React with Meteor

How can I add an upload image to a React form ?
<form onSubmit={this.handleSubmit.bind(this)} >
<label>Titre</label>
<input className="form-control"
type="text"
ref="title"
name="titleLoi"
value={this.state.titre}
onChange={this.handleChange.bind(this)}/>
<label>Description</label>
<input className="form-control"
type="text"
ref="abstract"
name="abstractLoi"
value={this.state.abstract}
onChange={this.handleChange.bind(this)}/>
<button className="btn btn-primary">Enregistrer</button>
</form>
I saw on Atmosphere a package okgrow:image-upload. Is it only using an external package that I can achieve this ?
Setting your form to handle file uploads by adding the enctype attribute and adding a file input will allow you to select a file to upload:
<form enctype="multipart/form-data" onSubmit={this.handleSubmit.bind(this)}>
<input className="form-control"
type="file"
ref="uploader"
name="image"
onChange={this.uploadFiles.bind(this)}/>
</form>
How you style this and handle this file on the server are different questions.

Handling application data as a frontend developer

So I'm working on building my first application in React. I really don't know much about databases, data models, schema, etc. Is it bad practice to just store my data in the browser as I develop then worry about hooking my app up to something like firebase later?
For example, below I store some user data in an object called user and just go from there. As long as I have some way of accessing it later. Keeping in mind I'm in the beginning stages of frontend development is this bad practice?
Thanks!
var SignUpForm = React.createClass({
getUserInfo : function(event) {
event.preventDefault();
var user = {
name : this.refs.name.value,
userName : this.refs.userName.value,
email : this.refs.email.value,
password : this.refs.password.value
}
console.log(user)
this.refs.signUpForm.reset();
},
render : function() {
return (
<form onSubmit={this.getUserInfo} ref="signUpForm">
<div className="form-group">
<label>First and Last Name</label>
<input type="text" className="form-control" ref="name" />
</div>
<div className="form-group">
<label>Username</label>
<input type="text" className="form-control" ref="userName" />
</div>
<div className="form-group">
<label>Email Address</label>
<input type="email" className="form-control" ref="email" />
</div>
<div className="form-group">
<label>Password</label>
<input type="password" className="form-control" ref="password" />
</div>
<button type="submit" className="btn btn-primary">Sign Up</button>
</form>
);
}
});

Resources