Having trouble with my form submitting correctly.
I would like to have multiple checkbox values being passed through the form if one is selected but it is only sending one. Also, for some reason my first value is being marked as “name” instead of setting. The code is below along with what’s being passed to Netlify Forms. Thanks for the help!
function encode(data) {
const formData = new FormData();
for (const key of Object.keys(data)) {
formData.append(key, data[key]);
}
return formData;
}
class CSM2020 extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
handleSubmit = e => {
e.preventDefault();
const form = e.target;
fetch("/", {
method: "POST",
body: encode({
"form-name": form.getAttribute("name"),
...this.state
})
})
.then(() => navigateTo(form.getAttribute("action")))
.catch(error => alert(error));
};
render() {
return (
<Layout location={this.props.location}>
<Helmet
htmlAttributes={{ lang: "en" }}
title="CSM 2020 | Advanced Travel Therapy"
meta={[
{
name: "description",
content:
"Enter to win our Travel Adventure pack!"
}
]}
/>
<div className="site-body page-content">
<h1>CSM 2020</h1>
<p>
Enter to win our Travel Adventure pack!
</p>
<div className="grid grid-gutters-lg">
<div className="grid-cell">
<form
name="CSM 2020"
method="post"
action="/csm-2020/"
data-netlify="true"
data-netlify-honeypot="bot-field"
onSubmit={this.handleSubmit}
>
<div className="grid grid-gutters-sides grid-full med-grid-1of2">
{/* The `form-name` hidden field is required to support form submissions without JavaScript */}
<input type="hidden" name="form-name" value="file-upload" />
<p hidden>
<label>
Don’t fill this out:{" "}
<input name="bot-field" onChange={this.handleChange} />
</label>
</p>
<div className="grid-cell">
<label>
First Name
<sup>*</sup>
<input
type="text"
id="firstNameField"
name="firstName"
placeholder=""
onChange={this.handleChange}
required
/>
</label>
</div>
<div className="grid-cell">
<label>
Last Name
<sup>*</sup>
<input
type="text"
id="lastNameField"
name="lastName"
placeholder=""
onChange={this.handleChange}
required
/>
</label>
</div>
<div className="grid-cell">
<label>
Email
<sup>*</sup>
<input
type="email"
id="emailField"
name="email"
placeholder=""
onChange={this.handleChange}
required
/>
</label>
</div>
<div className="grid-cell">
<label>
Phone
<sup>*</sup>
<input
type="tel"
id="phoneField"
name="phone"
pattern="[0-9]{3} [0-9]{3} [0-9]{4}"
maxlength="12"
placeholder=""
onChange={this.handleChange}
required
/>
</label>
</div>
<div className="grid-cell">
<label>
City
<sup>*</sup>
<input
type="text"
id="city"
name="City"
placeholder=""
onChange={this.handleChange}
/>
</label>
</div>
<div className="grid-cell">
<label>
State
<sup>*</sup>
<input
type="text"
id="state"
name="State"
placeholder=""
onChange={this.handleChange}
required
/>
</label>
</div>
<div className="grid-cell">
<label>
Anticipated Graduation Date
<sup>*</sup>
<input
type="date"
id="gradDate"
name="Graduation Date"
placeholder=""
onChange={this.handleChange}
required
/>
</label>
</div>
<div className="grid-cell">
<label>
Discipline
<sup>*</sup>
<select name="Discipline[]" value={this.state.value} onChange={this.handleChange} id="discipline">
<option value="PT" >Physical Therapist</option>
<option value="SLP" >Speech Language Pathologist</option>
<option value="OT" >Occupational Therapist</option>
<option value="PTA" >Physical Therapy Assistant</option>
<option value="SLPA" >Speech Language Pathologist Assistant</option>
<option value="COTA" >Occupational Therapy Assistant</option>
<option value="CF" >Clinical Fellow</option>
<option value="SP" >School Psychologist</option>
</select>
</label>
</div>
<div className="grid-cell u-full">
<label>
Settings Interested In
<sup>*</sup>
<br />
<div className="label-checkbox">
<input type="checkbox" name="setting[]" id="setting1" value="Acute" onChange={this.handleChange} />
<label className="label-inline" for="setting1">Acute</label>
<input type="checkbox" name="setting[]" id="setting2" value="Skilled Nursing Facility" onChange={this.handleChange} />
<label className="label-inline" for="setting2">Skilled Nursing Facility</label>
<input type="checkbox" name="setting[]" id="setting3" value="School" onChange={this.handleChange} />
<label className="label-inline" for="setting3">School</label>
<input type="checkbox" name="setting[]" id="setting4" value="Home Health" onChange={this.handleChange} />
<label className="label-inline" for="setting4">Home Health</label>
<input type="checkbox" name="setting[]" id="setting5" value="Outpatient" onChange={this.handleChange} />
<label className="label-inline" for="setting5">Outpatient</label>
<input type="checkbox" name="setting[]" id="setting6" value="Inpatient" onChange={this.handleChange} />
<label className="label-inline" for="setting6">Inpatient</label>
</div>
</label>
</div>
<div className="grid-cell u-full">
<button type="submit">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</Layout>
);
}
}
export default CSM2020;