I have a website here having two Forms. One under RESERVE VIP TABLE and another in the footer where it says JOIN OUR NEWSLETTER. I've used the following code to submit data from the FORM to Google Sheets. Question is that the first form is working fine but how can I submit the data from Newsletter section to Google Sheets in a separate Tab named "Subscribe"?
Newsletter HTML:
<form method="POST" name="google-sheet">
<input type="text" name="subscribeEmail" placeholder="Your Email Address">
<div class="gold-button small">
<button type="submit">Submit →</button>
<span class="bttn-border left virticle"></span>
<span class="bttn-border right virticle"></span>
<span class="bttn-border top horizontal"></span>
<span class="bttn-border bottom horizontal"></span>
</div>
</form>
Script I'm using for Reservation Form:
<script>
const scriptURL = 'https://script.google.com/macros/s/AKfycbzJA2cZGROFNCwLuPnJjECW21zXMZs8nT4Xo0Nzjy5ETlOVQ3gHgyjE3wzzmHky_0I_/exec'
const form = document.forms['google-sheet']
form.addEventListener('submit', e => {
e.preventDefault()
fetch(scriptURL, { method: 'POST', body: new FormData(form)})
.catch(error => console.error('Error!', error.message))
})
</script>
If it were me, I'd have to tackle it like this:
<form action="https://script.google.com/macros/s/[SCRIPT ID]/exec" method="post">
<input type="text" name="subscribeEmail" placeholder="Your Email Address">
<div class="gold-button small">
<button type="submit">Submit →</button>
<span class="bttn-border left virticle"></span>
<span class="bttn-border right virticle"></span>
<span class="bttn-border top horizontal"></span>
<span class="bttn-border bottom horizontal"></span>
</div>
</form>
function doPost(e) {
var ss = SpreadsheetApp.openById("SHEET ID");
var sheet = ss.getSheetByName("Subscribe");
// or use getRange().setValue() if something specific
sheet.appendRow([e.parameter.subscribeEmail])
}