I have an HTML form (generated with PHP) that is used to populate a Google Sheets document. Here's the relevant JavaScript:
<script>
const scriptURL = 'https://script.google.com/macros/s/blahblahblah/exec'
const form = document.forms['quiz_entry']
form.addEventListener('submit', e => {
e.preventDefault()
// Edit: here's the second bit I'm trying to achieve:
fetch("success.php", { method: 'POST', body: new FormData(form)})
// Here's the bit that sends the data (successfully) to Google Docs:
fetch(scriptURL, { method: 'POST', body: new FormData(form)})
.then(response => {
console.log('Success!', response);
var tn=document.getElementById("team_name").value;
document.getElementById('quiz_entry').reset();
window.location="success.php?tn="+tn;
})
.catch(error => console.error('Error!', error.message))
})
</script>
This works fine but I'd like to be able to send the form data to another PHP page as well (so I can enter the data into an SQLite database - I know how to do this). I've tried putting a second "fetch" in, with the URL a PHP file on the same webserver (localhost) but can't get the data to show ($_POST empty). I could send the form POST data directly (i.e. without the e.preventDefault() but want to keep the Google bit as a backup)
Edit: think the above actually works, I'm just calling "success.php" twice and the second time there is no $_POST data - by writing the data to a text file the first time around, I've proven that it works...
question from:
https://stackoverflow.com/questions/65923389/beginner-post-html-form-to-multiple-pages-scripts 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…