Below I have my HTML code that includes two forms, they are both the same except for the name field that only does what it is intended to do when a form is before it (doesn’t matter if it’s displayable or not).
<div class="contact-right">
<form class="contact-form" style="display: none;">
<input id="form-name" name="Name" type="text" placeholder="Your Name" required>
<input id="form-email" name="Email" type="email" placeholder="Your Email" required>
<textarea id="form-message" name="Message" type="text" placeholder="Your Message"></textarea>
<button id="send-button" type="submit">Send</button>
</form>
<form name="submit-to-google-sheet" class="contact-form">
<input id="form-name" name="Name" type="text" placeholder="Your Name" required>
<input id="form-email" name="Email" type="email" placeholder="Your Email" required>
<textarea id="form-message" name="Message" type="text" placeholder="Your Message"></textarea>
<button id="send-button" type="submit">Send</button>
</form>
</div>
</section>
<div class="footer">
<p>Stuff Here</p>
</div>
<script>
const scriptURL = '<SCRIPT URL>'
const form = document.forms['submit-to-google-sheet']
form.addEventListener('submit', e => {
e.preventDefault()
fetch(scriptURL, { method: 'POST', body: new FormData(form)})
.then(response => console.log('Success!', response))
.catch(error => console.error('Error!', error.message))
})
</script>
** I used to protect the URL I was using
The intended use is to have a single form that gets the text elements of the form inputs and stores it on a Google Sheet (followed: https://github.com/jamiewilson/form-to-google-sheets). I’ve tried so many angles and it always seems to be that the form with the submit-to-google-sheet doesn’t run if it’s the only form.
2
Answers
Turns out my javascript which was purposefully getting HTML elements by id would inhibit the actions that the form component was trying to do (I put in my own email checkers).
I ended up deleting the prior form and javascript portion of my code that references those components, I'm pretty sure because it detected the prior form the second one was unaffected.
I don’t think that this works because of the presence of another form. Please ensure the following:
delete the previous triggers and create a new trigger. In the github link you’ve given, there are two functions given. So ensure that the function you’re selecting must be doPost and not the other one.
Trigger must be on ‘onSubmit’. By mistake you should not select ‘onEdit’
Before deploying run the initial setup once.
Ensure the name of the sheet. It is given as ‘Sheet1’. Don’t rename it though I couldn’t see any relation between the sheet_name and the function
The headers of the sheet must exactly match the ‘name’ mentioned in each input tag.
Save it before publishing.
Publish as webapp.
Publish as a new version.
Access to anybody including anonymous
copy the new url given by oath and paste in the place of <SCRIPT_URL>. Ensure quotes both sides. (Both should be either single or either double. No mismatching)
Try placing the javascript inside the head tag. Ensure that it is inside script tags. Or place it along with other javascript in a separate js file. provide link to it.
Try this also. Don’t think that jquery and document-ready is useless. Sometimes, I’ve seen incidents that it does wonders with google appscript. This could be my perception even. But try. Provide a jquery Cdn link and place the code inside $( document ).ready(function() { })