2016-05-07 8 views
0

Wie sende ich ein Formular in diesem Code mit HTML und GS? Es wird nicht gesendet, wenn ich auf "Senden" klicke. Ich denke, es gibt einen Fehler mit der 'send()' Funktion, oder vielleicht hat das Formular Fehler. Bitte hinterlassen Sie eine Antwort. Danke im Voraus!Senden von Formularen mit HTML und Google Apps Script


HTML:

<!DOCTYPE html> 
 
<!-- Quiz By Yona Klatchko --> 
 
<html> 
 
    <head> 
 
    <base target="_top"> 
 
    </head> 
 
    <body> 
 
    <p class="BoldVerdana"> 
 
     Welcome to Yona's Geography Quiz. 
 
    </p> 
 
    <p class="BoldVerdana"> 
 
     Begin whenever you are ready. 
 
    </p> 
 
    <br/> 
 
    <script> 
 
    function send() { 
 
     var emailAddress = document.getElementById('emailAddress').value; 
 
     var result = document.getElementById('Questions').value; 
 
     google.script.run.send(emailAddress, result); 
 
    } 
 
    </script> 
 
    <style> 
 
     p.BoldVerdana { 
 
      font-weight: bold; 
 
      font-family: "Verdana"; 
 
     } 
 
    </style> 
 
    <br/> 
 
    <font face="Verdana"> 
 
     Your teacher's email address 
 
    </font> 
 
    <br/> 
 
    <br/> 
 
    <input type="name" id="emailAddress"/> 
 
    <br/> 
 
    <br/> 
 
    <div id="answers"> 
 
     <form name="Questions" id="Questions" method="post" action="EmailAddress; subject=Geography Quiz Answers enctype='text/plain'"> 
 
     <p class="BoldVerdana"> 
 
      Input questions 
 
     </p> 
 
     <font face="Verdana"> 
 
      1. Which state has a climate suitable for growing citrus fruits — California or Maine? 
 
     </font> 
 
     <br/> 
 
     <br/> 
 
     <input type="text" id="Ia1"> 
 
     <br/> 
 
     <br/> 
 
     <br/> 
 
     <font face="Verdana"> 
 
      2. The North Atlantic current brings warm waters from the tropics to the west coast of which continent? 
 
     </font> 
 
     <br/> 
 
     <br/> 
 
     <input type="text" id="Ia2"> 
 
     <br/> 
 
     <br/> 
 
     <br/> 
 
     <font face="Verdana"> 
 
      3. Which Canadian province produces more than half of the country's manufactured goods? 
 
     </font> 
 
     <br/> 
 
     <br/> 
 
     <input type="text" id="Ia3"> 
 
     <br/> 
 
     <br/> 
 
     <br/> 
 
     <font face="Verdana"> 
 
      4. To visit the ruins of Persepolis, an ancient ceremonial capital of Persia, you would have to travel to what present-day country? 
 
     </font> 
 
     <br/> 
 
     <br/> 
 
     <input type="text" id="Ia4"> 
 
     <br/> 
 
     <br/> 
 
     <br/> 
 
     <font face="Verdana"> 
 
      5. What is the term for a part of an ocean or sea that cuts far into the bordering landmass and may contain one or more bays? 
 
     </font> 
 
     <br/> 
 
     <br/> 
 
     <input type="text" id="Ia5"> 
 
     <br/> 
 
     <br/> 
 
     <br/> 
 
     <p class="BoldVerdana"> 
 
      Multiple choice 
 
     </p> 
 
     <font face="Verdana" id="Ma1"> 
 
      6. Which one of the following zones of the atmosphere is rich in Ozone gas? 
 
      <br/> 
 
      <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Mesosphere"> 
 
      Mesosphere 
 
      <br/> 
 
      <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Troposphere"> 
 
      Troposphere 
 
      <br/> 
 
      <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Ionosphere"> 
 
      Ionosphere 
 
      <br/> 
 
      <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Stratosphere"> 
 
      Heat is generated in it 
 
\t \t <br/> 
 
\t \t <br/> 
 
\t \t </font> 
 
     <font face="Verdana" id="Ma2"> 
 
      7. Troposphere is the hottest part of the atmosphere because 
 
      <br/> 
 
      <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="It is closest to the sun"> 
 
      It is closest to the sun 
 
      <br/> 
 
      <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="It is heated by the Earth's surface"> 
 
      It is heated by the Earth's surface 
 
      <br/> 
 
      <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="There are charged particles in it"> 
 
      There are charged particles in it 
 
      <br/> 
 
      <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="Heat is generated in it"> 
 
      Heat is generated in it 
 
\t \t <br/> 
 
\t \t <br/> 
 
     </font> 
 
     <font face="Verdana" id="Ma3"> 
 
      8. Continents have drifted apart because of 
 
      <br/> 
 
      <input type="radio" name="8. Continents have drifted apart because of" value="Volcanic eruption"> 
 
      Volcanic eruption 
 
      <br/> 
 
      <input type="radio" name="8. Continents have drifted apart because of" value="Folding and Faulting of rocks"> 
 
      Folding and Faulting of rocks 
 
      <br/> 
 
      <input type="radio" name="8. Continents have drifted apart because of" value="Tectonic activities"> 
 
      Tectonic activities 
 
      <br/> 
 
      <input type="radio" name="8. Continents have drifted apart because of" value="All of the above"> 
 
      All of the above 
 
      <br/> 
 
     </font> 
 
     <br/> 
 
     </form> 
 
    </div> 
 
    <button onclick="send(); Submit" value="Submit"> 
 
     <font face="Verdana" size="10"> 
 
     Submit 
 
     </font> 
 
    </button> 
 
    </body> 
 
</html>

GS:


Send():

function send(emailAddress, result) { 
    MailApp.sendEmail(emailAddress, result) 
} 

doGet:

function doGet() { 
    return(HtmlService.createTemplateFromFile('Quiz.html').evaluate().setTitle('Geo Quiz').setSandboxMode(HtmlService.SandboxMode.IFRAME)); 
} 
+0

Nun verwenden zunächst die Entwicklertools Ihres Browsers um den Code zu überprüfen und sehen, welche Fehler Pop-up in der Konsole. Nicht sicher, warum Ihr On-Click-Attribut "send(); Submit" ist, sollte wahrscheinlich den Submit-Teil entfernen – Gerneio

Antwort

0

Zwei Parameter können nicht an den Server gesendet werden. Sie haben:

google.script.run.send(emailAddress, result); 

werden sollten:

google.script.run.send([emailAddress, result]);//Put variables into an array 

Sie kein method oder action Attribut im <form> Tag nennen sollten, wenn Sie gehen google.script.run.myFunctionName() zu verwenden.

Sie haben:

<form name="Questions" id="Questions" method="post" action="EmailAddress; subject=Geography Quiz Answers enctype='text/plain'"> 

werden sollten:

<form id="Questions"> 

Auch Sie kein name Attribut in Form Element-Tag benötigen. Sie erhalten das Formularobjekt nach ID, was Sie tun sollten.

Wenn Sie das gesamte Objekt form abrufen und das Objekt form an den Server senden möchten, sollten Sie die Eigenschaft value nicht verwenden.

Sie haben:

var result = document.getElementById('Questions').value; 

werden soll:

var result = document.getElementById('Questions');//Get the Form object 

Wenn das E-Mail-Eingabefeld innerhalb der Form war, dann könnte man das gesamte form Objekt, und Sie würden nicht brauchen um die E-Mail-Adresse einzeln zu erhalten. Sie haben:

var emailAddress = document.getElementById('emailAddress').value; 

Wenn Sie außerhalb des Formularobjekts die E-Mail-Adresse Eingabefeld tun wollen, dann ich erklären, wie das am Ende zu tun. Wenn Sie das Formularobjekt senden möchten, muss jedes <input> ein name Attribut haben. Sie haben keine Namensattribute in allen Formulareingabeelementen.

Wenn Apps Script das Formularobjekt an den Server sendet, verarbeitet es zunächst das Objekt und entfernt alles außer dem name-Attribut als Schlüssel und dem Wert als Wert im Schlüssel/Wert-Element des Objekts. Sie können keine Werte aus dem Formularobjekt auf dem Server anhand des ID-Namens abrufen.

Sie haben Submit im <button> Element:

<button onclick="send(); Submit" value="Submit"> 

Es sollte:

<button onclick="send()"> 

Sie brauchen nicht ein value Attribut, weil das Wort "Senden" ist in zwischen den beiden button Tags.

Die Serverfunktion kann keine zwei Parameter empfangen. Sie haben:

function send(emailAddress, result) { 

werden sollten:

function send(theFormObject) { 
    MailApp.sendEmail(theFormObject.nameOfInput, result) 
} 

Sie verwenden sehr langen Namen im name Attribut:

function send(theFormObject) { 

Die Server-Funktion sein sollte

Sie haben :

Es scheint, als ob Sie entweder ein placeholder oder ein Eingabe-Label möchten.

<input type="radio" placeholder="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Mesosphere"> 

Oder:

<label for="questionSix">6. Which one of the following zones of the atmosphere is rich in Ozone gas?</label> 
<input type="radio" value="Mesosphere" id="questionSix"> 

Wenn Sie die E-Mail-Eingabefeld nicht innerhalb des <form> setzen wollen, dann gibt es ein Problem, weil Sie nicht zwei separate Werte an die gesendet werden, Server. Sie können jedoch das Formular Objekt stringify:

var result = document.getElementById('Questions');//The Form object 
var myFormObject = JSON.stringify(result); 
var emailAddress = document.getElementById('emailAddress').value; 

Und dann eine lange Zeichenfolge an den Server senden:

var bothEmailAndOjbect = emailAddress + "," + myFormObject; 
google.script.run.send(bothEmailAndOjbect); 
Verwandte Themen