These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Submitting twice from one button

A.Stice
Registered: Mar 3 2010
Posts: 12

Hello,

I am working on a form that needs to be able to submit twice, once to a web url and another to an email address. The problem that I am having is getting this to happen from a single button click. Any ideas on how I can accomplish this?

Thanks!

[EDIT]I was able to find the solution to my problem by stumbling across it in a URL tutorial on this site[/EDIT]

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Can you tell us which tutorial?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
Actually - the solution I thought I found didn't work - I couldn't even get it to submit the one time following the tutorial exactly, much less a second time. I was using the URL encoding strings tutorial.
Here's a bit of a better description of what I am trying to do:

I'm working on a form to track the progress of a server installation. The form will be initially filled out by a requester and then will be send to our team. There are 6 stages in the installation, and each stage is handled by a different person, so I need to be able to change this url each time. Also, each time the form is updated and submitted, it also needs to be updated in a SharePoint documents library. I have all the code working to update the workflow, I'm just not able to get the submissions working. I'm using LiveCycle ES and JavaScript, by the way.

Thanks!
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
So, the first question is do you have the URL submit working by itself? If the answer is yes, then all you need to do is to combine the two submits. Of course they need to be done with two different submit operations. Have you tried this already? Something like this

event.target.submitForm(...URL Submit...);
event.target.submitForm(...Email Submit...);

If you have tried this and it doesn't work it may be a timing problem. The submit is a non-blocking operation. Meanging that Acrobat pretty much runs both of them at the same time. This could of course be a problem. If you already tried this and it doesn't work there is a possible workaround.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
I have not started trying to upload the form to the Sharepoint list using post yet - instead I threw together a local php page just to test that the submit was working correctly. I did what you said - using what I saw on the URL string encoding tutorial and event.target.submitForm. Right now it is submitting to and being handled by the php page correctly, but when it goes to submit to the email address it never sends and never opens my mail client. Here's the code I am using now:

var requestNumber = xfa.form.form1.requestNumber.rawValue;SaveAsTarget = "requestForm"+requestNumber+".pdf";event.target.LCB_SaveAs(event.target); var strURL = "http://localhost:120/handlepdf.php";var strEncodURL = encodeURI(strURL);event.target.submitForm({cURL:strEncodURL, cSubmitAs: "PDF"}); var strSubject = "Request Form Submission";var strMsg = "Request form number "+requestNumber+" has been submitted. \n Reference the attached form or http://localhost:120/"+SaveAsTarget;strURL = "mailto:<span class="spamspan"><span class="u">email</span> [at] <span class="d">domain [dot] com</span></span>?subject=" + strSubject;strURL += "&body=" + strMsg;strEncodURL = encodeURI(strURL);event.target.submitForm({cURL:strEncodURL, cSubmitAs: "PDF"});
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Very good! so you can successfully submit to an URL, but that is only part of the process.

Can you verify that the final submitForm is actually being run, that there are no exceptions between the first and second submits?

Can you verify that the second submit operates correctly by itself?

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
The second submit does not operate properly by itself - however it is executing (Acrobat pops up and asks for permission to submit to that address). I don't know if I'm using the javascript debugger properly - but the console is not showing any errors thrown. It just hangs for a while after I allow the connection and then goes back to the form.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The hang is probably Acrobat connecting to the first submit URL.

Place a couple of console.println() statments in the code to monitor the process.

It's possible that because the submit is non-blocking that you can't do two, one right after the other. In the past I've setup multiple submits by using data returned from the first submit to trigger the second one. So they are effectivly decoupled. For example, place an invisible field on your form. Put the second submit in the validate event of this field, possibly qualified with the data entered. Setup your PHP script to return data to this field.

But before trying this out, make sure that running both one after the other is really the problem. Another approach, for testing anyway, is to use app.setTimeOut() to run the second submit sometime after the first.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
Making progress. I got the multiple submits working properly but I didn't like that it asked permission for submit twice (needed to send to two urls) so I wrote a webpage to accept the file as a single submission and then forward that on to as many other webpages or emails as is needed. This also fixed my folder level javascript problem as I can easily rename the file as part of the web service.

The only other thing that I need to fix is that when I use event.target.submitForm it doesn't verify against any mandatory fields I have. It will submit regardless of mandatory field values. Is there a function I should call to force it to verify against the fields?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Brialliant Solution!! As a security measure Acrobat burps when a document contacts resources in two different locations, not just submission URLs. You can silence the alert by marking the PDF as trusted. But I like your solution better.

Do you mean you wrote a server script? Can you post your solution, or a link. I think a lot of people would be interested. Submitting is a pain in a butt.

You can write your own form level validation to run before allowing the submit. I do this quite a bit because customers don't like how the built-in functionality works. There have been quite a few posts here that provide code for doing it. But there is another solution that does use the built-in Submit. Since you are now submitting to a known stable URL why not use the built-in submit button. If you need to do this indirectly you can hide the real submit button and hit it from JavaScript with the executeEvent("Click") function. You can even modify the submission parameters from a script before running the click event.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

****

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
I was afraid I'd have to check everything myself. Not too big of a deal but it's a rather dense form so I was hoping I could just use the built in check to save myself some work :)

I don't mind posting my solution at all. I used ASP.net in the end as it's what we run our production servers off of but if anyone would rather use PHP (more free php hosts than asp.net hosts) I don't mind sharing how I did this in PHP either.

The code is just a basic ASP.net page that accepts url parameters. The two important parameters are a filename and a destination, or multiple destinations as in my case. I ended up using two emails because using the email upload to a sharepoint list was easier than trying to submit using Sharepoint integration.

I generated a file name string in the pdf when the button was clicked, then submitted to my url using:

var strURL = "http://ourdomain.com/Emailer.aspx?fileName="+fileName+"&destination="<span class="spamspan"><span class="u">+destination</span> [at] <span class="d">ourdomain [dot] com</span></span>;var strEncodURL = endcodeURI(strURL);event.target.submitForm({cURL:strEncodURL, cSubmitAs: "PDF"});

The asp code is fairly simple as well. I basically processed the file on page load, saved it to disk as the filename provided and then emailed it to the destinations. The code was:

protected void Page_Load(object sender, EventArgs e){try{String filename = Request.QueryString["FileName"];String destination = Request.QueryString["destination"];MailMessage email = new MailMessage();email.To.Add(destination);email.From = new MailAddress("<span class="spamspan"><span class="u">OurServer</span> [at] <span class="d">ourdomain [dot] com</span></span>");email.Subject = "New PDF From Submission";email.Body = "A PDF from has been submitted. Please refer to the attached file.";email.Priority = MailPriority.High;email.IsBodyHTML = true;int numread = Request.ContentLength;byte[] formData = Request.BinaryRead(numRead);MemoryStream tempStream = new MemoryStream(formData);Attachment attach = new Attachment(tempStream, filename);email.Attachments.Add(attach); SmtpClient smtp = new SmtpClient();try{smtp.Send(email);} catch (Exception exc){lblError.Text = "Send failure: "+exc.ToString();} finally{lblGood.Text = "Your Email has been sent successfully - Thank You";}} catch (Exception exc){lblError.Text = "Error setting up SMTP: "+exc.toString();}

From here you can easily add more destinations by just repeating the email.To.Add(), or by generating a new email object.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Very nice and simple!! Thanks a bunch.

The only issue I see with the code is that it's writing the PDF data out to an actual disk file. I'm not familiar with the Attachment object but I'd think you could do it all in memory. I prefer not to create files if I don't have too.

Also, it looks like it's returning HTML? How is that working out? Any issue with Acrobat not liking the return data?

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
This is returning html data, which actually works out very well in Acrobat. Acrobat simply displays a render of the returned data, which in this case is a confirmation that the submission was received. I also agree that you should be able to attach the file directly from memory, but I haven't been able to figure that one out yet.

[EDIT] Actually - I just figured it out. The trick was to use a MemoryStream instead of a FileStream. Once I found it I kicked myself it was so simple. I'll edit my previous post when I get the code working. [/EDIT]

[EDIT2] Updated the code in my previous post [/EDIT2]
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I figured it was an option on the Attachemnt constructor. .Net is pretty good about having all the options.

Forgot to mention in my last post that you don't need to test every field. You can loop though them all and just test the required ones. Like this:
var rgEmpty = /^\s*$/; function validateForm(){var aReqFlds= [];//loop through all pages in the formfor (var nPage=0; nPage< xfa.host.numPages; nPage++){//loop through fields in the pagevar oFields = xfa.layout.pageContent(nPage, "field", false);var nNodes = oFields.length; for (var n=0; n<nNodes; n++){//if the field is required, Include check for Radio groupvar oTstFld = null;if(oFields.item(n).mandatory == "error")oTstFld = oFields.item(n);else if(oFields.item(n).parent.mandatory == "error")oTstFld = oFields.item(n).parent; if(oTstFld){if (rgEmpty.test(oTstFld.rawValue) || (oTstFld.rawValue==null)||(oTstFld.resolveNode("ui.checkButton") && oTstFld.rawValue==0) ){aReqFlds.push(oTstFld.name);}}}} if (aReqFlds.length > 0){xfa.host.messageBox("These Required fields are empty:\n\n" + aReqFlds.join("\n");}elseevent.target.submitForm(...);}

There are several ways to do this. For example the code could highlight the field by changing the background color.

And you can also use the built-in submit and modify the URL with any dynamic data on the mouse down event .

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

A.Stice
Registered: Mar 3 2010
Posts: 12
Yeah, I did roughly the same thing for checking field values already. Your's looks a little more efficient than mine, so I'll probably use that. Well - I now have a fully working form. I may put together an example project for the .net side of the solution for if anybody needs it. Thanks for your help, you guys are very helpful here.