Solved

Need to combine JavaScript funtions from 2 buttons

Posted on 2014-07-24
6
219 Views
Last Modified: 2014-07-25
Hi,
How can I combine JavaScript functions from 2 buttons?

<input name="Submit" type="submit" onclick="javascript:window.print()" value="Print Details" />

<input type="button" class="button green" onClick="if(checkFields()){location.href='process_payment.php?cartId=<?php echo $cookie;?>'}" value="Process Order" >

Open in new window



I would like my print button to have an alert "please note that the order details have not been processed yet, do you want to process this order now?"

and if they click ok then execute  "if(checkFields()){location.href='process_payment.php?cartId=<?php echo $cookie;?>'}"



I was also wondering if it is better to move location.href='process_payment.php?cartId=<?php echo $cookie;?> to the checkFields function and can I add an animated gif to this function?

<script language="JavaScript" type="text/javascript" >
   //<!--
  var clickcount = 0;

   function checkFields(){
     if (clickcount == 0) {
       //count clicks 
       clickcount ++;
       return true;
     }
     else {
       alert("Transaction already in progress.. Please wait...");
       return false;
     }
   }
   //-->

</script>

Open in new window

0
Comment
Question by:sabecs
  • 3
  • 2
6 Comments
 
LVL 15

Assisted Solution

by:Insoftservice
Insoftservice earned 400 total points
Comment Utility
What do you mean by animated gif.
Please elaborate little more for that

<script language="JavaScript" type="text/javascript" >
   //<!--
  var clickcount = 0;

   function checkFields(cookie){


var r = confirm("please note that the order details have not been processed yet, do you want to process this order now?");
if (r == true) {
    location.href='http://lovetomarry.com/process_payment?cartId='+cookie;

} else {
    //return false;
} 

  
   }
</script>
  <a href="javascript:checkFields('cookie')">Test</a>

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 100 total points
Comment Utility
<input name="Submit" type="button" id="PrintDetails" value="Print Details" />

<input type="button" class="button green" id="ProcessOrder" value="Process Order" data-cartid="<?php echo $cookie;?>"  />

Open in new window


window.onload = function() {
    document.getElementById("PrintDetails").onclick = function() {
          var ok = window.confirm("please note that the order details have not been processed yet, do you want to process this order now?");
          if(ok) {
              var done = processOrder();
              if(done) {
                  document.getElementsByTagName("form")[0].submit();
              }
          }
          else {
              window.print();
              document.getElementsByTagName("form")[0].submit();
          }
    }
    var processOrder = function() {
            var cart =  this.getAttribute("data-cartid").value;
            if(checkFields()) {
                location.href = 'process_payment.php?cartId=' + cart;
            }
    }
    document.getElementById("ProcessOrder").onclick = processOrder;
}

Open in new window

0
 

Author Comment

by:sabecs
Comment Utility
Thanks fro your comments..

Regarding the animated gif, just wondering if there is a wait to display a message that the order is in progress which may take 10-20 seconds, I was wondering if I can display a div with an animated image inside it  via a function?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Accepted Solution

by:
Insoftservice earned 400 total points
Comment Utility
Its little css and jquery work.

Just add line in my comments


   function checkFields(cookie){

    document.getElementById('loader').display="block";
var r = confirm("please note that the order details have not been processed yet, do you want to process this order now?");
if (r == true) {

....
}else{
....
}
document.getElementById('loader').display="none";
}

Html part

<div id="loader" style="display:none;position:absolute;top:0%;left:0%;width:100%;height:2500px;background-color:#EFEAEA;z-index:1001;-moz-opacity:0.8;opacity:.80;filter:alpha(opacity=80);">
        <table style="margin-top:300px" width="789" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr><td width="763" align="center" valign="top"><img src="loading.gif" border="0" /></td></tr>
        </table>
    </div>
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
so you change location(url) of the page and during this order process you want to display a GIF.
if the GIF and Javascript is in the page and you change location of the page how this can be possible?
0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
@leakim971 i think he wants loader image till his clients click ok or cancel button.
@sabecs Please correct me if i am wrong
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now