谷歌表单响应无法从网站上工作

Google Form Response not working from the website

本文关键字:网站 工作 表单 响应 谷歌      更新时间:2023-09-26

我正在使用 Google 表单值与我的网站集成,我想在其中提交表单并将数据存储在 google 表格中作为表单响应。我正在使用 AJAX 重定向到另一个页面,而不是 Google 表单提交页面。但是每当我尝试提交时,它都会准确地重定向到我的页面,但数据不会保存在 Google 表格中。

这是我的代码:

<strong>Full Name</strong>
<input type="text" name="Fullname" class="form-control" id="Fullname" />
<strong>Email Address</strong>
<input type="text" name="Email" class="form-control" id="Email" />
<strong>Subject</strong>
<input type="text" name="Subject" class="form-control" id="Subject" />
<strong>Details</strong>
<textarea name="Details" rows="8" cols="0" class="form-control" id="Details"></textarea><br />
<button type="button" id="btnSubmit" class="btn btn-info" onclick="postContactToGoogle()">Submit</button>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
    function postContactToGoogle() {
        var email = $('#Email').val();
        var fullname = $('#FullName').val();
        var subject = $('#Subject').val();
        var details = $('#Details').val();
        $.ajax({
            url: "https://docs.google.com/forms/d/abcdefgh1234567xyz/formResponse",
            data: {
                "entry_805356472": fullname,
                "entry_1998295708": email, "entry_785075795":
                subject, "entry_934055676": details
            },
            type: "POST",
            dataType: "xml",
            statusCode: {
                0: function () {
                    window.location.replace("Success.html");
                },
                200: function () {
                    window.location.replace("Success.html");
                }
            }
        });
    }
</script>

如何将数据保存在 Google 表格中?我的代码中是否缺少某些内容?

您可以

直接从 google view form 页面复制表单,如演示所示,然后在 AJAX 调用中进行以下更改,如下所示。现在,一旦您提交数据,它就会在谷歌表单中可见,查看回复。

$(function(){
    $('input:submit').on('click', function(e){
      e.preventDefault();
        $.ajax({
            url: "https://docs.google.com/forms/d/18icZ41Cx3-n1iW7yTOZdiDx9a6mySWHOy9ryd1l59tM/formResponse",
            data:$('form').serialize(),
            type: "POST",
            dataType: "xml",
            crossDomain: true,
            success: function(data){
              //window.location.replace("youraddress");
              //console.log(data);
            },
            error: function(data){
              //console.log(data);
            }
        });
    });
  });
<div class="ss-form"><form action="https://docs.google.com/forms/d/18icZ41Cx3-n1iW7yTOZdiDx9a6mySWHOy9ryd1l59tM/formResponse" method="POST" id="ss-form" target="_self" onsubmit=""><ol role="list" class="ss-question-list" style="padding-left: 0">
<div class="ss-form-question errorbox-good" role="listitem">
<div dir="auto" class="ss-item  ss-text"><div class="ss-form-entry">
<label class="ss-q-item-label" for="entry_1635584241"><div class="ss-q-title">What's your name
</div>
<div class="ss-q-help ss-secondary-text" dir="auto"></div></label>
<input type="text" name="entry.1635584241" value="" class="ss-q-short" id="entry_1635584241" dir="auto" aria-label="What's your name  " title="">
<div class="error-message" id="1979924055_errorMessage"></div>
<div class="required-message">This is a required question</div>
</div></div></div>
<input type="hidden" name="draftResponse" value="[,,&quot;182895015706156721&quot;]
">
<input type="hidden" name="pageHistory" value="0">
<input type="hidden" name="fvv" value="0">
<input type="hidden" name="fbzx" value="182895015706156721">
<div class="ss-item ss-navigate"><table id="navigation-table"><tbody><tr><td class="ss-form-entry goog-inline-block" id="navigation-buttons" dir="ltr">
<input type="submit" name="submit" value="Submit" id="ss-submit" class="jfk-button jfk-button-action ">
</td>
</tr></tbody></table></div></ol></form></div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

如果您在Google上转到表单并单击"查看实时表单",然后在表单上查看源代码,您将看到要上传的字段在表单entry.12345678中具有name;id在表单entry_12345678中。 您需要使用 name 值。 试试这个:

<script>
    function postContactToGoogle() {
        var email = $('#Email').val();
        var fullname = $('#FullName').val();
        var subject = $('#Subject').val();
        var details = $('#Details').val();
        $.ajax({
            url: "https://docs.google.com/forms/d/abcdefgh1234567xyz/formResponse",
            data: {
                "entry.805356472": fullname,
                "entry.1998295708": email, 
                "entry.785075795":  subject, 
                "entry.934055676": details
            },
            type: "POST",
            dataType: "xml",
            statusCode: {
                0: function () {
                    window.location.replace("Success.html");
                },
                200: function () {
                    window.location.replace("Success.html");
                }
            }
        });
    }
</script>