1

I used the top answer to this question to build a form that feeds into a sheet along with file upload. Now I've hit another wall.

I have categories, and sub-categories. I'd like the sub-categories to only show up IF their parent category has been selected. I just can't figure out A) where I need to put the code (on our website it's right in with the HTML), I've tried putting it in the HTML file and the Code.gs file, or B) if the code I'm using is even right.

Here's the form - the "Co-Op Category" is the parent categories, I have hidden divs for each category that would hold the 'child categories'

HTML:

   <script>
  // Javascript function called by "submit" button handler,
  // to show results.
  function updateOutput(resultHtml) {
    toggle_visibility('inProgress');
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = resultHtml;
  }

  // From blog.movalog.com/a/javascript-toggle-visibility/
  function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'block')
      e.style.display = 'none';
    else
      e.style.display = 'block';
  }
</script>

<div id="formDiv">
<!-- Form div will be hidden after form submission -->
<form id="myForm">

    Name: <input name="name" type="text" /><br/>
    Co-Op Amount: <input name="amount" type="text" /><br/>
    Co-Op Split:<br />
    <input type="radio" name="split" value="100%">100&#37;<br>
    <input type="radio" name="split" value="50/50">50/50<br>
    <input type="radio" name="split" value="75/25">75/25<br>
    Other: <input type="text" name="split" /><br />
    Reason for Co-Op: <input name="reason" type="text" cols="20" rows="5" /><br />
    Brand:  
    <select name="brand">
    <option>Select Option</option>
    <option>Bluebird</option>
    <option>Brown</option>
    <option>Ferris</option>
    <option>Giant Vac</option>
    <option>Honda</option>
    <option>Hurricane</option>
    <option>Little Wonder</option>
    <option>RedMax</option>
    <option>SCAG</option>
    <option>Snapper Pro</option>
    <option>Sno-Way</option>
    <option>SnowEx</option>
    <option>Wright</option>
    <option>Ybravo</option>
    </select><br/>
    Co-Op Category:<br />
    <input type="radio" name="category" id="dealer" value="Dealer Advertising">Dealer Advertising<br />
    <input type="radio" name="category" id="online" value="Digital/Online Marketing">Digital/Online Advertising<br />
    <input type="radio" name="category" id="meetings" value="Meetings and Schools">Meetings and Schools<br />
    <input type="radio" name="category" id="advertising" value="PACE Advertising">PACE Advertising<br />
    <input type="radio" name="category" id="pricing" value="Program Pricing Promotions">Program Pricing Promotions<br />
    <input type="radio" name="category" id="correspondence" value="PACE-to-Dealer Correspondence">PACE-to-Dealer Correspondence<br />
     Other: <input type="text" id="other" name="category" /><br />

    <div class="dealer box" style="display: none;">DEALER</div> 
    <div class="online box" style="display: none;">ONLINE</div> 
    <div class="meetings box" style="display: none;">MEETINGS</div> 
    <div class="advertising box" style="display: none;">ADVERTISING</div> 
    <div class="pricing box" style="display: none;">PRICING</div>  
    <div class="correspondence box" style="display: none;">CORRESPONDENCE</div> 


    Email: <input name="email" type="text" /><br/>
    Message: <textarea name="message" style="margin: 2px; height: 148px; width: 354px;"></textarea><br/>
    School Schedule (Image Files Only): <input name="myFile" type="file" /><br/>
  <input type="button" value="Submit"
      onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
</form>
</div>

<div id="inProgress" style="display: none;">
<!-- Progress starts hidden, but will be shown after form submission. -->
Uploading. Please wait...
</div>

<div id="output">
  <!-- Blank div will be filled with "Thanks.html" after form submission. -->
</div>

Code.gs:

var submissionSSKey = '1zzRQwgXb0EN-gkCtpMHvMTGyhqrx1idXFXmvhj4MLsk';
var folderId = "0B2bXWWj3Z_tzTnNOSFRuVFk2bnc";

function doGet(e) {
  var template = HtmlService.createTemplateFromFile('Form.html');
  template.action = ScriptApp.getService().getUrl();
  return template.evaluate();
}


function processForm(theForm) {
  var fileBlob = theForm.myFile;
  var folder = DriveApp.getFolderById(folderId);
  var doc = folder.createFile(fileBlob);

  // Fill in response template
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  var name = template.name = theForm.name;
  var amount = template.amount = theForm.amount;
  var split = template.split = theForm.split;
  var reason = template.reason = theForm.split;
  var brand = template.brand = theForm.brand;
  var category = template.category = theForm.category;
  var message = template.message = theForm.message;
  var email = template.email = theForm.email; 
  var fileUrl = template.fileUrl = doc.getUrl();

  // Record submission in spreadsheet
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheets()[0];
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 9).setValues([[name,amount,split,reason,category,brand,message,email,fileUrl]]);

  // Return HTML text for display in page.
  return template.evaluate().getContent();
}

//Toggle Secondary Categories
function(){
    $('input[type="radio"]').click(function(){
        if($(this).attr("id")=="dealer"){
            $(".box").not(".dealer").hide();
            $(".dealer").show();
        }
        if($(this).attr("id")=="online"){
            $(".box").not(".online").hide();
            $(".online").show();
        }
        if($(this).attr("id")=="advertising"){
            $(".box").not(".advertising").hide();
            $(".advertising").show();
        }
        if($(this).attr("id")=="pricing"){
            $(".box").not(".pricing").hide();
            $(".pricing").show();
        }
        if($(this).attr("id")=="correspondence"){
            $(".box").not(".correspondence").hide();
            $(".correspondence").show();
        }
        if($(this).attr("id")=="meetings"){
            $(".box").not(".meetings").hide();
            $(".meetings").show();
        }
        if($(this).attr("id")=="other"){
            $(".box").not(".other").hide();
            $(".other").show();
        }
    });
};

This bit specifically is where I'm having trouble:

//Toggle Secondary Categories
    function(){
        $('input[type="radio"]').click(function(){
            if($(this).attr("id")=="dealer"){
                $(".box").not(".dealer").hide();
                $(".dealer").show();
            }
            if($(this).attr("id")=="online"){
                $(".box").not(".online").hide();
                $(".online").show();
            }
            if($(this).attr("id")=="advertising"){
                $(".box").not(".advertising").hide();
                $(".advertising").show();
            }
            if($(this).attr("id")=="pricing"){
                $(".box").not(".pricing").hide();
                $(".pricing").show();
            }
            if($(this).attr("id")=="correspondence"){
                $(".box").not(".correspondence").hide();
                $(".correspondence").show();
            }
            if($(this).attr("id")=="meetings"){
                $(".box").not(".meetings").hide();
                $(".meetings").show();
            }
            if($(this).attr("id")=="other"){
                $(".box").not(".other").hide();
                $(".other").show();
            }
        });
    };
Community
  • 1
  • 1
crossbeats
  • 65
  • 1
  • 14
  • That jQuery code can't go into the Code.gs file. It needs to be inside of `` script tags. You can add a `console.log('it ran!')` statement to test for whether anything is actually running. You'll need to open up the browser developers console, and make sure the console tab is active. In Chrome you hit the f12 key. – Alan Wells Mar 02 '16 at 19:55
  • @SandyGood I added the jQuery to the – crossbeats Mar 02 '16 at 20:56
  • When the browser evaluates the HTML (and JavaScript and CSS) It's finding something out of place or missing. When that happens, everything from that point on, gets left out. So, if the error is in the code, there won't be any code in the browser to run, from the point of the error on. You will need to delete/cut sections of code out until you don't get the error, so that you can know where the error is happening. Unfortunately, it's hard to debug HTML from the browser with Apps Script. You can put all your HTML into something like Notepad++, and check for syntax errors. – Alan Wells Mar 02 '16 at 21:35

1 Answers1

2

The unexpected token is due to the function(){ line, which is invalid syntax for the jQuery document ready function. You should have:

$(function(){
    $('input[type="radio"]').click(function(){
    ...
    });
});

With that fixed, your next error will be:

Uncaught ReferenceError: $ is not defined

That's because you haven't included jQuery, which is what the $ symbol is referring to in statements like $(this). You'll want to read this for more tips about using jQuery in Google Apps Script. The short story, though: You need to add the following, adjusted for whatever version of jQuery you intend to use:

<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

Updated Form.html, which shows the appropriate <div> as you intended. It also includes the recommended doctype, html, head and body tags:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
</head>

<body>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>
  <script>
    // Javascript function called by "submit" button handler,
    // to show results.
    function updateOutput(resultHtml) {
      toggle_visibility('inProgress');
      var outputDiv = document.getElementById('output');
      outputDiv.innerHTML = resultHtml;
    }

    // From blog.movalog.com/a/javascript-toggle-visibility/
    function toggle_visibility(id) {
      var e = document.getElementById(id);
      if (e.style.display == 'block')
        e.style.display = 'none';
      else
        e.style.display = 'block';
    }

    //Toggle Secondary Categories
    $(function() {
      $('input[type="radio"]').click(function() {
        if ($(this).attr("id") == "dealer") {
          $(".box").not(".dealer").hide();
          $(".dealer").show();
        }
        if ($(this).attr("id") == "online") {
          $(".box").not(".online").hide();
          $(".online").show();
        }
        if ($(this).attr("id") == "advertising") {
          $(".box").not(".advertising").hide();
          $(".advertising").show();
        }
        if ($(this).attr("id") == "pricing") {
          $(".box").not(".pricing").hide();
          $(".pricing").show();
        }
        if ($(this).attr("id") == "correspondence") {
          $(".box").not(".correspondence").hide();
          $(".correspondence").show();
        }
        if ($(this).attr("id") == "meetings") {
          $(".box").not(".meetings").hide();
          $(".meetings").show();
        }
        if ($(this).attr("id") == "other") {
          $(".box").not(".other").hide();
          $(".other").show();
        }
      });
    });
  </script>

  <div id="formDiv">
    <!-- Form div will be hidden after form submission -->
    <form id="myForm">

      Name:
      <input name="name" type="text" /><br/>
      Co-Op Amount: <input name="amount" type="text" /><br/>
      Co-Op Split:<br />
      <input type="radio" name="split" value="100%">100&#37;<br>
      <input type="radio" name="split" value="50/50">50/50<br>
      <input type="radio" name="split" value="75/25">75/25<br> Other: <input type="text" name="split" /><br /> Reason for Co-Op: <input name="reason" type="text" cols="20" rows="5" /><br />
      Brand:
      <select name="brand">
        <option>Select Option</option>
        <option>Bluebird</option>
        <option>Brown</option>
        <option>Ferris</option>
        <option>Giant Vac</option>
        <option>Honda</option>
        <option>Hurricane</option>
        <option>Little Wonder</option>
        <option>RedMax</option>
        <option>SCAG</option>
        <option>Snapper Pro</option>
        <option>Sno-Way</option>
        <option>SnowEx</option>
        <option>Wright</option>
        <option>Ybravo</option>
      </select><br/>
      Co-Op Category:<br />
      <input type="radio" name="category" id="dealer" value="Dealer Advertising">Dealer Advertising<br />
      <input type="radio" name="category" id="online" value="Digital/Online Marketing">Digital/Online Advertising<br />
      <input type="radio" name="category" id="meetings" value="Meetings and Schools">Meetings and Schools<br />
      <input type="radio" name="category" id="advertising" value="PACE Advertising">PACE Advertising<br />
      <input type="radio" name="category" id="pricing" value="Program Pricing Promotions">Program Pricing Promotions<br />
      <input type="radio" name="category" id="correspondence" value="PACE-to-Dealer Correspondence">PACE-to-Dealer Correspondence<br />
      Other: <input type="text" id="other" name="category" /><br />

      <div class="dealer box" style="display: none;">DEALER</div>
      <div class="online box" style="display: none;">ONLINE</div>
      <div class="meetings box" style="display: none;">MEETINGS</div>
      <div class="advertising box" style="display: none;">ADVERTISING</div>
      <div class="pricing box" style="display: none;">PRICING</div>
      <div class="correspondence box" style="display: none;">CORRESPONDENCE</div>


      Email: <input name="email" type="text" /><br/>
      Message: <textarea name="message" style="margin: 2px; height: 148px; width: 354px;"></textarea><br/>
      School Schedule (Image Files Only): <input name="myFile" type="file" /><br/>
      <input type="button" value="Submit" onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
    </form>
  </div>

  <div id="inProgress" style="display: none;">
    <!-- Progress starts hidden, but will be shown after form submission. -->
    Uploading. Please wait...
  </div>

  <div id="output">
    <!-- Blank div will be filled with "Thanks.html" after form submission. -->
  </div>

</body>

</html>
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Thank you, thank you, thank you! For not only providing corrected code, but explaining it! – crossbeats Mar 03 '16 at 17:52
  • No problem - that's how good answers should be! Hopefully, after you've gotten the help _you_ need, others will be able to glean something from it to help their own situations. Welcome to the site! – Mogsdad Mar 03 '16 at 17:57