'Send a value via EmailJs without using a form
I want to send an EmailJs with two parameters, the email that comes from the form, and the let toysList, that comes from the localStorage(and converted into a string via .toString()). How can I do that? I know how to connect the email because is in the form, but I don't know where i should put the toysList that is a variable.
import React from "react";
import emailjs from "emailjs-com";
let toysList = window.JSON.parse(localStorage.getItem("cart")).toString();
function ClientSubmit() {
function sendEmail(e) {
e.preventDefault();
emailjs
.sendForm(
"...",
"...",
e.target,
"..."
)
.then(
(result) => {
console.log(result.text);
},
(error) => {
console.log(error.text);
}
);
e.target.reset();
alert(
"Email send successfully!"
);
}
return (
<div>
<h3>Give us your mail and we will give you the price of the items</h3>
<form onSubmit={sendEmail}>
<div className="col-4">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
name="email"
placeholder="[email protected]"
/>
</div>
<div className="mb-3 col-2 ">
<input
type="submit"
className="btn btn-primary"
value="Send"
></input>
</div>
</form>
</div>
);
}
Solution 1:[1]
Insert a hidden in the form containing the value of the toyList so when you submit the form the toyList will be there.
<form onSubmit={sendEmail}>
<div className="col-4">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
name="email"
placeholder="[email protected]"
/>
<input
type="hidden"
className="form-control"
name="toyList"
defaultValue={toyList}
/>
</div>
<div className="mb-3 col-2 ">
<input
type="submit"
className="btn btn-primary"
value="Send"
></input>
</div>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jorn Blaedel Garbosa |