Skip to main content
Participant
October 25, 2022
Answered

Submit a form vs. Javascript w/required fields

  • October 25, 2022
  • 2 replies
  • 1211 views

I have a form that will be submitted as a complete PDF.  The form has required fields.  Tried "Submit a Form" action but I cannot include specific fields from the form in the email subject. 

 

I switched to a Javascript function to submit the form; now I get the specificity of the email I desire (with field answers included in the subject line) but the required fields no longer prevent the form from being sent if they are not filled.  

 

Is there a way to get one method or the other to do both?

1.  Not submit form via email unless required fields are filled 

2. Place specific field answers in subject of submission email

 

mailto:email@server.com?subject=CONTENTOFSUBJECT&body=CONTENTOFBODY

OR

var targetEmail = "email@server.com";

var subjectLine = "name" + " " + this.getField("CLINIC NAME").valueAsString + " " + this.getField("DATE").valueAsString ;

var body = "CONTENT OF EMAIL";

this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});

This topic has been closed for replies.
Correct answer try67

It should be:

 

if (validateRequiredFields(this)) {

// all of the code to email the file should go here

}

2 replies

Thom Parker
Community Expert
Community Expert
October 25, 2022

Read this:

https://acrobatusers.com/tutorials/submitting-data/

 

It shows how to use the "submitForm" function, which validates required fields and allows for the email "subject", "to", and "message" text to be set. And it will submit the entire PDF.   

 

You could also use the same code you've posted with the "mailDoc" function, but modified to validate the required fields in the script, as Try67 suggests. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
October 25, 2022

#2 is not possible if you use the "Submit a Form" command, but it is if you open that URL using the launchURL method, but that won't solve your main problem (validating required fields).

#1 is possible. I've posted code here that does that validation with a script. Search for "validateRequiredFields".

Participant
November 22, 2022

Hello,  I found the code you are referencing and tried to combine it with the code I already had to create an email with fields in the subject line.  I'm attempting to prevent an email from popping up if there are empty required fields, but now if they have filled out correctly my code produces two emails.  How do I combine so that only one email is produced?   Thank you, 

 

if (validateRequiredFields(this)) this.mailDoc({cTo: "XYZ@server.com"});

function validateRequiredFields(doc) {

var emptyFields = [];

for (var i=0; i<doc.numFields; i++) {

var f = doc.getField(doc.getNthFieldName(i));

if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {

emptyFields.push(f.name);

}

}

if (emptyFields.length>0) {

app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));

return false;

}

return true;

}
var targetEmail = "XYZ@server.com";

var subjectLine = "OD Order Form" + " " + this.getField("CLINIC NAME").valueAsString + " " + this.getField("DATE").valueAsString ;

var body = "TEXT";

this.mailDoc({cTo: targetEmail, cSubject: subjectLine, cMsg: body});

 

 

Bernd Alheit
Community Expert
Community Expert
November 22, 2022

Replace the first mailDoc with the lines at the end.