Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

javascript - (beginner) Post HTML form to multiple pages/scripts

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...