HTML表单到Google电子表格 [英] HTML form to Google spreadsheet

查看:56
本文介绍了HTML表单到Google电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个HTML表单,该表单将数据提交到Google电子表格.本教程向我展示了窍门,尽管对我来说效果很好包括一个确认页面,向用户返回他/他提供的信息.我以某种方式找到了一种方法,但是我无法使确认页面包含用户提供的信息.

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.

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

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.

这是我的代码:

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

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>

非常感谢.

推荐答案

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

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

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

请更正为它.

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

帖子请求已发送至 ="a href =" https://script.google.com/blabla"rel =" nofollow noreferrer> https://script.google.com/blabla 毫无疑问在他的标题中有表单数据.因此,您可以在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>");

这篇关于HTML表单到Google电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆