Formsubmit.co form not working in reactapp - reactjs

I added formsubmit service to my email form on my react application simple personal website. but it does not seem to be working. It does nothing. Does the extra javascript in there mess with the formsubmit service? I actually put my real email in just changed it for this post.
import React, { useState } from 'react';
import './styles.contactform.css';
function ContactForm() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [message, setMessage] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
console.log(name, email, message)
setName('')
setEmail('')
setMessage('')
}
return (
<div className="contact-container">
<form className="contact-form" action="https://formsubmit.co/myemail#email.com" method="POST" onSubmit={handleSubmit}>
<input type="hidden" name="_next" value="https://notmicahclark.herokuapp.com/"/>
<input type="text" value={name} id="name" name="name" placeholder="Name..." onChange={(e) => setName(e.target.value)} required/>
<input type="email" value={email} id="email" name="email" placeholder="Email..." onChange={(e) => setEmail(e.target.value)} required/>
<img className="letter-img" src="contact_form_imagepng.png" alt="Mail Letter"/>
<input id="message" value={message} name="message" placeholder="Your message..." onChange={(e) => setMessage(e.target.value)}/>
<button type="submit" value="Submit">Submit</button>
</form>
</div>
);
}
export default ContactForm;

To overcome this issue, you can use the AJAX form feature provided by FormSubmit.
According to their documentation:
You can easily submit the form using AJAX without ever having your
users leave the page. — this even works cross-origin.
Please refer to their documentation about AJAX forms (sample code snippets are available): https://formsubmit.co/ajax-documentation

I've tested your code. The problem is that you are both using the form's HTML action field, and at the same time using your own method for form submission. Remove the action=... call and instead write this in your handleSubmit method.
fetch('https://formsubmit.co/ajax/myemail#email.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(Object.fromEntries(new FormData(event.target))),
})
Should work like a charm. Add then and catch statements to your liking.

Related

Supabase signup with react state email and password

I am trying to implement the standard Supabase signup, using react-jsx, but keep getting the response:
"You must provide either an email or phone number and a password"
My code looks as follows:
const [login, setLogin] = useState('')
const [password, setPassword] = useState('')
const signUpSubmitted = () => {
supabase.auth
.signUp({ login, password })
.then((response) => {response.error ? alert(response.error.message) : setToken(response)})
.catch((err) => { alert(err)})
}
and the form:
<form id='sign-up'>
<h3>Sign Up</h3>
<label>Email:</label>
<input
type='email'
value={login}
onChange={(e) => setLogin(e.target.value)}
/>
<label>Password:</label>
<input
type='password'
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<input onClick={signUpSubmitted} type='submit'/>
</form>
I assume the problem lies with me attempting to save the values in a state, before passing them to the database. I don't see why it should be a problem, they are both strings from what I understand, so maybe I'm way off.
According to the Supabase docs, you need to pass email instead of login.
const { data, error } = await supabase.auth.signUp({
email: 'example#email.com',
password: 'example-password',
})
I would also suggest a few other optimizations:
Move your submit handler to the <form /> in order to support submitting the form with the Enter key.
Add event.preventDefault() to your submit handler to prevent the default form browser redirection behavior.
Change your submit input to a more semantic <button />.
Link your labels and inputs together with htmlFor and id attributes for accessibility (just make sure they're unique ids).
Updated Component:
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [token, setToken] = useState();
const handleSubmit = (event) => {
event.preventDefault();
supabase.auth
.signUp({ email, password })
.then((response) => {
response.error ? alert(response.error.message) : setToken(response)
})
.catch((err) => { alert(err) });
}
return (
<form id="sign-up" onSubmit={handleSubmit}>
<h3>Sign Up</h3>
<label htmlFor="email">Email:</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<label htmlFor="password">Password:</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Sign Up</button>
</form>
);

How to send form data to email in React JS?

I have to send form datas to an email address.
How can I do?
I did some research and I understand that you have to use a library to do this. correct?
What do you suggest?
I think this one might be worth looking into - https://www.emailjs.com/docs/sdk/send-form/
If you are looking for implementation for Reactjs then use like this :
// install #emailjs/browser
import React, { useRef } from 'react';
import emailjs from '#emailjs/browser';
export const ContactUs = () => {
const form = useRef();
const sendEmail = (e) => {
e.preventDefault();
// service_id, templte_id and public key will get from Emailjs website when you create account and add template service and email service
emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', form.current,
'YOUR_PUBLIC_KEY')
.then((result) => {
console.log(result.text);
}, (error) => {
console.log(error.text);
});
};
return (
<form ref={form} onSubmit={sendEmail}>
<label>Name</label>
<input type="text" name="user_name" />
<label>Email</label>
<input type="email" name="user_email" />
<label>Message</label>
<textarea name="message" />
<input type="submit" value="Send" />
</form>
);
};

How to pass input value to another react component

I'm making a react project for practice, where I have multiple forms, distributed across multiple pages and I'm using react components for each page with the help of react routing. I have a submit page in the end where the user is able to press the button and I need all the form values from previous pages so that I can use it as parameters to POST it on a REST API. Since I'm new to react, I'm having a hard time figuring out how to pass all these data to this submit page component, any help or advice would be appreciated!
This is one component for one of the pages, for instance :
Personal.jsx
function Personal(){
const text = "You watch “What? Where? When?” Yeah. Our founders used to play it. That’s where they got a question about a famous American author and screenwriter Ray Bradbury. Albeit, our CEO Gaga Darsalia forgot the exact name and he answered Ray Redberry. And at that moment, a name for a yet to be born company was inspired - Redberry 😇"
const [fName, setfName] = useState("");
const [lName, setlName] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
return (
<div className="personal-container">
<div className="leftDiv">
<h1 className="coordinates-h1">Hey, Rocketeer, what are your coordinates?</h1>
<div className="inputDiv">
<form>
<Input value={fName} type="text" onChange={(e) => setfName(e.target.value)} required placeholder="First Name"/>
<Input value={lName} type="text" onChange={(e) => setlName(e.target.value)} required placeholder="Last Name"/>
<Input value={email} type="email" onChange={(e) => setEmail(e.target.value)} required placeholder="E Mail"/>
<Input value={phone} onChange={(e) => setPhone(e.target.value)} pattern="[5]{1}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}" type="tel" placeholder="+995 5_ _ _ _"/>
</form>
</div>
<div>
<PersonalSVG />
</div>
</div>
<div className="rightDiv">
<h1 className="origins-h1">Redberry Origins</h1>
<p className="origins-p">{text}</p>
</div>
</div>
)
}
export default Personal;
I need to pass these input values to the submit page : Submit.jsx :
function Submit(){
const style = {
color:"white",
textDecoration : "none"
}
const onSubmit = (e) => {
console.log("submitted");
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
"token": "76977183-c0e1-4f49-b51d-34e590260b10",
"first_name": "gela",
"last_name": "gelashvili",
"email": "gelashvili#gela.ge",
"phone": "+995591933382",
"skills": [
{
"id": 1,
"experience": 3
}
],
"work_preference": "from_home",
"had_covid": true,
"had_covid_at": "2022-02-23",
"vaccinated": true,
"vaccinated_at": "2022-02-23",
"will_organize_devtalk": true,
"devtalk_topic": "I would ...",
"something_special": "I am special!"
})
};
fetch('https://bootcamp-2022.devtest.ge/api/application', requestOptions)
}
return (
<div className="submitDiv">
<Link style={style} to="/thanks"><button onClick={onSubmit} className="submitButton">Submit</button></Link>
<div><a><Link style={style} to="/about"><h2>go back</h2></Link></a></div>
</div>
)
}
export default Submit;
I'm looking for replacing the custom strings in the post body, with actual input values. Thanks in advance

React: capturing form values that were not changed

In the tutorials I've found, the best way to capture forms inputs, is to use a "onchange" handler.
However, how should we deal with fields that are left to the default value ?
For example in something like the example below, if "country" is left to the default value, the "formData" will never get it.
const [formData, setFormData] = useReducer(formReducer, {});
const handleChange = event => {
setFormData({
name: event.target.name,
value: event.target.value,
});
}
const handleSubmit = event => {
event.preventDefault();
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
};
fetch('http://0.0.0.0:8000/add_user/', requestOptions)
}
<form onSubmit={handleSubmit}>
<fieldset>
<input type="text" name="name" placeholder="Name" onChange={handleChange} />
<input type="text" name="country" value="" onChange={handleChange} required />
</fieldset>
<Button type="submit">Submit</Button>
</form>
I've thought about some solutions but I don't think they are the best practices. For example, adding a snippet of code that manually assigns all default values to the form in the construction, so the formData is pre-filled.
Is there a clean & nice way to do it ?
In your useReducer hook you should store initial country value and then assign it to the value in country input, like below:
const [formData, setFormData] = useReducer(formReducer, { country: "" });
<form onSubmit={handleSubmit}>
<fieldset>
<input type="text" name="name" placeholder="Name" onChange={handleChange} />
<input type="text" name="country" value={formData.country} onChange={handleChange} required />
</fieldset>
<Button type="submit">Submit</Button>
</form>

React Netlify form does not submit

Im making a personal website for training and I wanted to use Netlify form submission for /contact URL I tried adding just netlify , netlify='true', and other things like that but i just gives me 404 error everytime im trying to use Netlify form submission, but it doesnt seem to work no matter what I do, I followed several tutorials and repos but still does not work. It just gives 404 error and other stuff;
and please check my github repo,
My GitHub Repo's Link
and here is my Contact.js component;
import React, { Fragment, useState } from 'react';
import '../../App.css';
const encode = (data) => {
return Object.keys(data)
.map((key) => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
.join('&');
};
const Contact = () => {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [text, setText] = useState('');
const handleSubmit = (e) => {
fetch('/', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: encode({
'form-name': 'contact',
name: name,
email: email,
text: text,
}),
})
.then((res) => {
console.log(res);
})
.catch((error) => alert(error));
e.preventDefault();
};
return (
<Fragment>
<div className='contact'>
<div className='contact-wrapper'>
<div className='title'>
<span>Contact me</span>
</div>
<div className='contact-form'>
<form
className='myform'
method='POST'
name='contact'
data-netlify='true'
onSubmit={(e) => handleSubmit(e)}
>
<input type='hidden' name='form-name' value='contact'></input>
<input
type='text'
placeholder='Your Name'
name='name'
value={name}
onChange={(e) => setName(e.target.value)}
></input>
<input
type='email'
placeholder='Your E-Mail'
name='email'
value={email}
onChange={(e) => setEmail(e.target.value)}
></input>
<textarea
placeholder='Your message'
name='message'
value={text}
onChange={(e) => setText(e.target.value)}
></textarea>
<button type='submit'>Send</button>
</form>
</div>
</div>
</div>
</Fragment>
);
};
export default Contact;
Netlify's forms work by their servers editing your html at deploy to add in their hooks for submitting the form.
With a React app, the form will not be visible in the html at deploy, so this won't work.
Netlify have a guide for how to set up their forms with SPAs, view it here: https://www.netlify.com/blog/2017/07/20/how-to-integrate-netlifys-form-handling-in-a-react-app/

Resources