Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

javascript - HTML <input> Required Attribute Fails to Prevent Form from Submitting in Apps Script App

I'm testing some code I wrote in Google Apps Script. I've required my fields to have text, but when I test it with null fields, the sever side code is run anyway. The code fires the pop-up stating that fields are required, but submits the form when clicking "OK" on the pop-up. I've tested it where I've filled out all the fields and then submitted which uploads perfectly. I think I just have my coding backwards or something in my "onclick". I have a basic knowledge of coding so I'm sorry if this is a dumb question. Thank you, thank you, thank you in advance.

<p>
<form id="myForm">
  <h1>NHD Paper Upload</h1>
  <label>Name</label>
  <input type="text" name="myName" class="required" placeholder="Enter your full name..">
  <label>Division</label>
  <input type="text" name="myDivision" class="required" placeholder="(ex. Junior or Senior)">
  <label>School</label>
  <input type="text" name="mySchool" class="required" placeholder="Enter your school..">
  <label>Affiliate</label>
  <input type="text" name="myAffiliate" class="required" placeholder="Enter your affiliate..">
  <label>Select file to upload. Make sure your file is labeled in the following manner <b>LastName_Division_School_State.pdf</b></label>
  <input type="file" name="myFile">
  <input type="submit" value="Submit File" 
       onclick="validateForm();
                this.value='Please be patient while your paper is uploading..';
                google.script.run.withSuccessHandler(fileUploaded)
                .uploadFiles(this.parentNode);
                return false;">
  <br />
  <label><b>Once upload is successful please stay on this window to copy and paste the URL produced on the next screen into registration.</b></label>
   <br />
  <label><b>If you have any issues or questions please send an email to <a href="mailto:[email protected]">[email protected]</a>.</b></label>
</form>

</p>

<div id="output"></div>

<script>
    function validateForm() {
    var x=document.getElementsByClassName('required');
    for(var i = 0; i <x.length; i++){
       if (x[i].value == null || x[i].value == "")
       {
          alert("All fields must be filled out.");
          return false;
          }
       }
    }
    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').innerHTML = status;
    }

</script>

<style>
 input { display:block; margin: 15px; }
 p {margin-left:20px;}
</style>

and here is the javascript

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {

  try {

    var dropbox = "NHD Papers";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(dropbox);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + form.myName + ", Division: " + form.myDivision + ", School: " + form.mySchool + ", State: " + form.myState);

    return "<h2>File uploaded successfully!</h2><p>Copy and paste the following URL into registration:<br /><br /><strong>" + file.getUrl() + '</strong></p>';

  } catch (error) {

    return error.toString();
  }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Right now, google.script.run is being called directly from the submit button.

Current set up:

<input type="submit" value="Submit File" 
   onclick="validateForm();
            this.value='Please be patient while your paper is uploading..';
            google.script.run.withSuccessHandler(fileUploaded)
            .uploadFiles(this.parentNode);
            return false;">

If you want to prevent google.script.run from being run when a required input field is not filled in, I'd try running the submit event from the <form> tag.

<form id="myForm" onsubmit="validateForm();
            this.value='Please be patient while your paper is uploading..';
            google.script.run.withSuccessHandler(fileUploaded)
            .uploadFiles(this);
            return false;">

Make sure to change this.parentNode to just this, for using this set up.

As a personal preference, I like to put google.script.run it's own function. You are already using a separate function for validateForm(), you could put the google.script.run in that function:

Simplify the form tag to:

<form id="myForm" onsubmit="validateForm()">

Script

function validateForm() {
  var x=document.getElementsByClassName('required');
  for(var i = 0; i <x.length; i++){
    if (x[i].value == null || x[i].value == "")
    {
      alert("All fields must be filled out.");
      return false;
     }
      this.value='Please be patient while your paper is uploading..';
      var myFormObject = document.getElementById('myForm');
      google.script.run.withSuccessHandler(fileUploaded)
        .uploadFiles(myFormObject);

   }
}

Since the function is outside of the form, you can't use this.parentNode anymore. Get the form by your id. One option is shown in the example code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...