Solved

print dialog window on form submit button

Posted on 2007-04-11
2
580 Views
Last Modified: 2008-02-01
I have a javascript for a form that handles errors for things such as empty fields, unchecked radio buttons, etc.  However, on the submit button, if all criteria is met, then I need the print dialog window to come up before going to the action page.  The print dialog should print the form pages.  Any ideas how I can adjust what I have?  I also tried adding onClick="window.print()" to the submit button but it didn't work.  Again, the dialog has to appear only if all criteria is met in javascript below.

<script language="JavaScript">
      <!--//
      function valwpp(thisform) {
      
                                 myOption1 = -1;
                  for (i=0; i<thisform.newapp.length; i++) {
                  if (thisform.newapp[i].checked) {
                  myOption1 = i;
                  }
            }
            if (myOption1 == -1) {
                  alert("Please select either a New Application or Re-entry. (#1)");
                  return false;
            }
            if (document.formdata.licfname.value == "")
            {
                  alert("Please your first name. (#2)");
                  document.formdata.fname.focus();
                  return false;
            }
      return;
}

// -->
</script>


<form onSubmit="return valwpp(this)" action="formdata.cfm" name="formdata" method="post">
[fields]..
<input type="submit" value="Submit/Print" name="submit" src="images/submit.jpg"  >
</form>
0
Comment
Question by:COwebmaster
2 Comments
 
LVL 19

Accepted Solution

by:
dakyd earned 500 total points
ID: 18892102
What about changing this:
  <form onSubmit="return valwpp(this)"

to:
  <form onSubmit="if (valwpp(this)) window.print(); else return false;"

and then at the end of your function, make sure it does:
  return true;  (instead of just return;)

All it does is look at the return value of your function.  If it's true, it does a window.print() .  Otherwise, it returns false to stop the form submission.  Hope that helps.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18894373
Put the print() method at the end of validation function.
Like this:

<script language="JavaScript">
      <!--//
function valwpp(thisform) {
     myOption1 = -1;
     for (i=0; i<thisform.newapp.length; i++) {
         if (thisform.newapp[i].checked) {
            myOption1 = i;
         }
      }
      if (myOption1 == -1) {
         alert("Please select either a New Application or Re-entry. (#1)");
         return false;
      }
      if (thisform.licfname.value == ""){
          alert("Please your first name. (#2)");
          thisform.fname.focus();
          return false;
      }
      window.focus();
      window.print();
      return true;
}

// -->
</script>


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now