Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Submit a form vs. Javascript w/required fields

New Here ,
Oct 25, 2022 Oct 25, 2022

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});

TOPICS
Acrobat SDK and JavaScript
978
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 22, 2022 Nov 22, 2022

It should be:

 

if (validateRequiredFields(this)) {

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

}

Translate
Community Expert ,
Oct 25, 2022 Oct 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".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 22, 2022 Nov 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});

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2022 Nov 22, 2022

Replace the first mailDoc with the lines at the end.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2022 Nov 22, 2022
LATEST

It should be:

 

if (validateRequiredFields(this)) {

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2022 Oct 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines