簡體   English   中英

使用JavaScript PHP jQuery將網頁中的數據發布到Google表格

[英]Posting data from webpage using JavaScript PHP jQuery to Google Sheet

在您說這是重復的之前,請聽我說。 我遵循了這個答案( PHP的jQuery Ajax POST示例 ),但是我試圖將數據發送到Google表格。 我已經在Google表格上使用了該代碼,因為可以通過直接從該表格運行代碼來向其中添加數據。

但是,使其與我的網頁一起使用顯然是問題所在。 盡管我已經遵循了上面發布的問題的答案,但是我認為這里還有更多的原因與我在該領域工作的經驗不足有關。

下面的代碼是一個浮動頁腳,包含在整個網頁的代碼中(其中jquery-1.11.0.min.js和jquery-migrate-1.2.1.min.js已被調用)。 當我單擊“提交”按鈕時,頁面看起來像是在處理請求,但是Google表格Sheet1上什么都沒有出現( https://docs.google.com/spreadsheets/d/19l2kSHdBKEWtIFX44FxLBdvCoKjy7VqPF4IW6C1xAZc/edit?usp=sharing ) 。

#document
<html>
<body>
<div class="floater-footer" id="the-floater-footer">
<span id="myTestSpan"></span>
  <div class="row">
    <div class="col-md-1 col-sm-2 col-xs-3"><p>Newsletter</p></div>
    <div class="col-md-8 col-sm-7 col-xs-9">

      <form id="floater_footer" class="validate subscribe_form">
          <div id="subscrive_group wow fadeInUp">

          <!-- <input type="email" value="" name="EMAIL" class="form-control subscribe_mail" id="mce-EMAIL" placeholder="email address" required /> -->
          <input type="text" class="form-control subscribe_mail" id="bar" name="bar" value="" placeholder="email address" required />
          <!-- <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="subscr_btn subscribe_btn"> -->
          <input type="submit" value="Subscribe" class="subscr_btn subscribe_btn" />

          </div>
      </form>
    </div>
    <div class="col-md-3 col-sm-3 hidden-xs">
      <div class="pull-right">
        <!-- social media icons here -->
       </div>
    </div>
  </div>
</div>

<script type="text/javascipt">

jQuery( document ).ready(function( $ ) {
// variable to hold request
var request;
// bind to the submit event of our form
$("#floater_footer").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);
    //$('#result').text('Sending data...');

    // fire off the request to /form.php
    request = $.ajax({
        url: "https://script.google.com/macros/s/AKfycbyQIDmSInumcrNmU4zxIa4pV8tIlN3A9zx5L5o1hH4qNdP9nDw/exec",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        //$('#result').html('<a href="https://docs.google.com/spreadsheets/d/10tt64TiALYhPMqR2fh9JzkuhxW7oC0rXXPb_pmJ7HAY/edit?usp=sharing" target="_blank">Success - see Google Sheet</a>');
        console.log("Hooray, it worked!");
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault();
});
});

</script>

</body>
</html>

關於可能出什么問題的任何建議? 與本網站上的大多數網站相比,我在Web開發方面經驗不足。 因此,我確定這是我所缺少的簡單東西!

我不完全理解為什么它起作用,但是取出腳本並將其放在頁面的頂部,HTML似乎起作用了! 可能是因為腳本在元素之后出現是一個問題,或者是在頁面主要HTML內的HTML內包含$(document).ready()引起的,或者是其他原因! 不管是哪一種,以下解決的問題。

在頁面HTML開頭的代碼頂部(但在聲明jQuery.js文件之后),我放置了已經看到的腳本,但是做了一些外觀上的更改。 如果確實發生了一些我沒有意識到的重要更改,我將其放在此處。 我還將顯示腳本之前包含的兩個jQuery文件:

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>


<script type="text/javascript">
//this is for writing the newsletter signup out to a spreadsheet for the floating footer

$( document ).ready(function( $ ) {
// variable to hold request
var request;
// bind to the submit event of our form
$("#floater_footer").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);
    //$('#result').text('Sending data...');


    if( $('#email1').val() == "Email Received!" ) {

        //do nothing
        //basically, they pressed the button again on accident

    } else {

        // fire off the request to the Google Sheet script
        request = $.ajax({
            url: "https://script.google.com/macros/s/AKfycbyQIDmSInumcrNmU4zxIa4pV8tIlN3A9zx5L5o1hH4qNdP9nDw/exec",
            type: "post",
            data: serializedData
        });

    }


    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        //$('#result').html('<a href="https://docs.google.com/spreadsheets/d/10tt64TiALYhPMqR2fh9JzkuhxW7oC0rXXPb_pmJ7HAY/edit?usp=sharing" target="_blank">Success - see Google Sheet</a>');
        $('#email1').val('Email Received!');
        console.log("Newsletter signup complete!");
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault();
});
});

</script>

在HTML主體的最下方是這段代碼,用於放置我的頁腳:

    #document
<html>
<body>
<div class="floater-footer" id="the-floater-footer">
<span id="myTestSpan"></span>
  <div class="row">
    <div class="col-md-1 col-sm-2 col-xs-3"><p>Newsletter</p></div>
    <div class="col-md-8 col-sm-7 col-xs-9">

      <form id="floater_footer" class="validate subscribe_form">
          <div id="subscrive_group wow fadeInUp">

          <input type="text" class="form-control subscribe_mail" id="email1" name="emailSignup" value="" placeholder="email address" required />

          <input type="submit" value="Subscribe" class="subscr_btn subscribe_btn" />

          </div>
      </form>
    </div>
    <div class="col-md-3 col-sm-3 hidden-xs">
      <div class="pull-right">
        <!-- bunch of social media links/icons -->
       </div>
    </div>
  </div>
</div>
</body>
</html>

希望這將對將來有此問題的人有所幫助。 您可能會看到這樣做的原因,但我沒有。

非常感謝傑伊·布蘭查德Jay Blanchard)向我提出疑問,我的腳本是否甚至已經觸發,這激發了我將腳本拉出並放到其他地方的念頭! 該腳本幾乎可以肯定根本沒有觸發,並且是問題的根源。 但是那是為什么,我不知道。 但我希望這對其他人有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM