简体   繁体   中英

HTML form to Google spreadsheet

I am working on an HTML form that submits data to a Google spreadsheet. This tutorial showed me the trick and works perfect to me though it doesn't include a confirmation page returning the user the information she/he provided. Somehow I found a way but I cannot make the confirmation page include the information given by the user.

I found several questions close to this one, but most get the confirmation solved with innerHTML (little text added at the bottom/top of the form, not my intention) and/or I couldn't make them work.

This is the code I have:

</head>
<body>
    <form name="submit-to-google-sheet">
      <input name="email" type="email" placeholder="Email" required><br>
      <input name="nombre" type="text" placeholder="Nombre"><br>
      <input name="apellido" type="text" placeholder="Apellido"><br>

      <button type="submit">Enviar</button>
    </form>
    
    <script>
      const scriptURL = 'https://script.google.com/blabla'
      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))
        window.location.href = "conf.php";
      })
    </script>
</body>

And the confirmation page (conf.php):

<head>
    
</head>
<body>
    <p>Text.</p>
    <?php
    setlocale(LC_TIME,"es_ES");
    
    echo 'Nombre: ' . $_POST ["nombre"] . '<br>';
    echo 'Apellido: ' . $_POST ["apellido"] . '<br>';
    echo 'E-mail: ' . $_POST ["email"] . '<br><br>';
    echo strftime("Fecha de inscripción: %A %e de %B de %Y a %H:%M") . '<br>';
    ?>
    <button onclick="imprimir()">Imprimir confirmación</button>
    <p>Text.</p>
    <button onclick="volver()">Volver</button>
    <script>
      function imprimir() {
        window.print();
      }
      function volver()     {
        window.location.href = "index.html";
      }
    </script>
</body>

Thanks very much in advance.

There is a javascript syntax error. The following can't be compiled.

function(window.location.href = "confirm.php";)

Please correct it to.

window.location.href = "confirm.php";

The post request was sent to https://script.google.com/blabla having the form data in his header with no doubt. So you can control this request at confirm.php and echo the result like following.

//confirm.php
<?php
//get any kinds of data from https://script.google.com/blabla
...
//response the result
echo("<p>Successfully posted!</p><a onclick='window.location.history.back()'>Go back</a>");

You cannot have multiple lines within arrow functions without wrapping them with curly brackets, so you need to correct the fetch :

fetch(scriptURL, {
    method: 'POST',
    body: new FormData(form)
  })
  .then(response => {
      console.log('Success!', response);
      window.location.href = "conf.php";
    }
  )
  .catch(error => console.error('Error!', error.message))

Also, you will most likely always get an error, since you are sending a request to the fake endpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM