HTML格式的Google电子表格

问题描述 投票:0回答:2

我正在处理一个HTML表单,该表单将数据提交给Google电子表格。 This tutorial向我展示了技巧,并且对我来说非常完美,尽管它不包含确认页面,该页面返回了用户提供的信息。我以某种方式找到了一种方法,但是我无法使确认页面包含用户提供的信息。

[我发现了几个与此问题类似的问题,但是大多数问题都通过innerHTML(在表单底部/顶部添加的小文本,不是我的意图)解决了确认,并且/或者我无法使它们起作用。

这是我的代码:

</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>

和确认页面(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>

非常感谢。

javascript html forms
2个回答
1
投票

存在JavaScript语法错误。以下内容无法编译。

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

请更正为。

window.location.href = "confirm.php";
毫无疑问,

发布请求已发送至https://script.google.com/blabla,其标题中包含表单数据。因此,您可以在confirm.php中控制此请求,并回显如下结果。

//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>");

0
投票

[箭头功能内不能有多行而不用大括号括起来,因此需要更正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))

而且,由于您正在向伪造的端点发送请求,因此您很可能总是会收到错误。

© www.soinside.com 2019 - 2024. All rights reserved.