简体   繁体   English

成功提交到Google表格后,提交表单应显示消息

[英]Submit Form should display message after successful submit to Google Sheets

I am trying to submit a form to Google Sheets. 我正在尝试向Google表格提交表单。 This step is successful and I am getting all the numbers in the sheet. 此步骤成功完成,我将获得工作表中的所有数字。 Now I want to display a success message after the form is submitted and the result from Google Sheets is ok. 现在,我想在提交表单并且Google表格的结果正常后显示成功消息。 How do I do it? 我该怎么做?

I have tried various messages in javascript and ajax, but don't have much understanding about it. 我已经在javascript和ajax中尝试了各种消息,但是对此没有太多的了解。 I have shown my code below so you can have a look at it. 我在下面显示了我的代码,因此您可以看一下。

<form name="submit-to-google-sheet">
                          <select class="form-control custom-form" name="country_code" id="country_code">
                            <option name="country_code" value="+91">India 91</option>
                            <option name="country_code" value="+93">Afghanistan 93</option>
                          </select>
                        </div>
                      </div>
                      <div class="row">
                        <div class="form-group spec-col col-md-8 col-lg-8 col-sm-12">
                          <input type="text" class="form-control custom-form" id="phone_no" name="phone_no" placeholder="Enter the Number">
                        </div>
                    </div>
                      <button class="btn btn-submit" id="submit" type="submit">Submit</button>
                      </div>
                    </form>

For Submitting the content to Google Sheets, here is the code 对于将内容提交到Google表格,下面是代码

<script>
      const scriptURL = 'GOOGLE SHEETS URL'
      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))
      })
    </script>

After the form is successful, I can see a success message in the console. 表单成功后,我可以在控制台中看到成功消息。 Instead, I want to show a simple one-word line in HTML, that thanks a lot. 相反,我想在HTML中显示一个简单的单字行,非常感谢。 Your submission is successful, once the response from Google Sheets is Ok and hide the form. 一旦Google表格的回复确定并隐藏表单,您的提交便成功了。

Edit: The rest of the code can be referenced from here: https://github.com/jamiewilson/form-to-google-sheets 编辑:其余代码可从此处引用: https : //github.com/jamiewilson/form-to-google-sheets

Bellow the button create a blank response message 按下按钮后,创建空白响应消息

tag just like this 像这样标记

<button class="btn btn-submit" id="submit" type="submit">Submit</button>
<p id="response_message"></p>

Now in the scripts make the following change in the fetch function: 现在,在脚本中对fetch函数进行以下更改:

form.addEventListener('submit', e => {
    e.preventDefault()
    var response_message = document.getElementById("response_message");
    fetch(scriptURL, { method: 'POST', body: new FormData(form)})
      .then(response => response_message.innerHTML = "Success!")
      .catch(error => response_message.innerHTML = "Error!")
  })

If you are using a Google Form to submit the data to the Google Sheet there is a setting in the Forms settings where you can display a response on submit. 如果您使用Google表单将数据提交到Google表格,则表单设置中有一个设置,您可以在其中显示提交时的回复。 No code needed. 无需代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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