Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email received but content is blank #39

Closed
ghost opened this issue Jul 16, 2016 · 2 comments
Closed

Email received but content is blank #39

ghost opened this issue Jul 16, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Jul 16, 2016

I have followed the instructions but when I test the form and press "Submit" I get this on the browser:

{"result":"success","data":"{}"}

And this in the Email:

{}

This is what I entered in the form:

Name: test 123
Email: test@test.com
Message: this is a test message

Here is the form HTML:

<div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content modal-popup">
            <a href="#" class="close-link"><i class="icon_close_alt2"></i></a>
            <h3 class="white">Contact Techpubs</h3>
            <form action="https://script.google.com/macros/s/AKfycbwedovtuJ5dGWLH4agdQcjxsqWJaOz92KPZmiQxAlGIt3Ik7Ds/exec" method="POST" class="popup-form">
                <input type="text" class="form-control form-white" placeholder="Full Name">
                <input type="text" class="form-control form-white" placeholder="Email Address">
                <!--<input type="textarea" class="form-control form-white" placeholder="How can we help you?"> -->
                <textarea class="form-control form-white" rows="3" placeholder="How can we help you?"></textarea>

                <button type="submit" class="btn btn-submit">Submit</button>
            </form>
        </div>
    </div>
</div>

And here is the script.gs:

/******************************************************************************

  • This tutorial is based on the work of Martin Hawksey twitter.com/mhawksey *
  • But has been simplified and cleaned up to make it more beginner friendly *
  • All credit still goes to Martin and any issues/complaints/questions to me. *
    ******************************************************************************/

var TO_ADDRESS = "khwang@a10networks.com"; // change this ...

function doPost(e) {

try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
record_data(e);
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}

// new property service GLOBAL
var SCRIPT_PROP = PropertiesService.getScriptProperties();
// see: https://developers.google.com/apps-script/reference/properties/

/**

  • select the sheet
    */
    function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
    }

/**

  • record_data inserts the data received from the html form submission
  • e is the data received from the POST
    */
    function record_data(e) {
    Logger.log(JSON.stringify(e)); // log the POST data in case we need to debug it
    try {
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName('responses'); // select the responses sheet
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = [ new Date() ]; // first element in the row should always be a timestamp
    // loop through the header columns
    for (var i = 1; i < headers.length; i++) { // start at 1 to avoid Timestamp column
    if(headers[i].length > 0) {
    row.push(e.parameter[headers[i]]); // add data to row
    }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    }
    catch(error) {
    Logger.log(e);
    }
    finally {
    return;
    }

}

Appreciate any assistance you can give! Thanks!

@ghost ghost closed this as completed Jul 16, 2016
@mckennapsean
Copy link
Collaborator

So, this issue was closed with no explanation of a solution. Can you please do so, in case others run into similar issues in the future?

Side note for other collaborators on this repository... we get a LOT of these issues, which are effectively support based, rather than real issues with the code. This complicates the issues. Has there been any consideration to a forum for people to ask for help? e.g. Google Groups or Stack Overflow? A lot of the questions are very, very basic though, about how to do web development in general, which may be out of our scope. It may be worthwhile to consider some templates for these issues. Most are not replicable, either. :\

@ghost
Copy link
Author

ghost commented Jul 16, 2016

The issue was that I did not have a "name" attribute in the form fields. Below is the corrected HTML:

Contact Techpubs

<textarea name="message" class="form-control form-white" rows="3" placeholder="How can we help you?"></textarea> Submit

When the form is submitted, the spreadsheet will populate a "name", "email", and "message" column. Perhaps this should have been obvious to me but it might be a good point to drop into the tutorial.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant