ajax - Javascript two submit buttons in one form almost complete -


i have form 2 submit buttons onclick events attached. form action paypal form.

if make payment button pressed form emailed , redirects paypal payment.

if invoice button pressed email sent form details default action of redirecting paypal stopped.

it seems work fine when invoice buttin pressed still redirects paypal.

if invoice button pressed want send email stop , not continue on form action.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="paymentform" name="paymentform"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="currency_code" value="gbp"> <fieldset id="your_details" class="">  <legend>your details</legend>  <ol>   <li><label for="your_name">your name</label><input type="text" name="your_name" value="" id="your_name" class="large "></li>   <li><label for="your_position">your position</label><input type="text" name="your_position" value="" id="your_position" class="large"></li>   <li><label for="your_company">your company</label><input type="text" name="your_company" value="" id="your_company" class="large "></li>   <li><label for="your_telephone">your telephone</label><input type="text" name="your_telephone" value="" id="your_telephone" class="medium "></li>   <li><label for="your_mobile">your mobile</label><input type="text" name="your_mobile" value="" id="your_mobile" class="medium"></li>  </ol> </fieldset> <input type="submit" value="pay credit card &rarr;" id="cc" onclick="return email_me(document.paymentform);" class="submit_button"> <input type="submit" value="pay invoice &rarr;" id="invoice" class="submit_button" onclick="return email_me(document.paymentform,true);"> 

var xmlhttp; var formname; var invoiceonly_var function email_me(form,invoiceonly) {     formname = form.name;     var params = "";     var i;     var element;     var type;     var name;     var fieldvalue;     var optional;     var badfields = "";     var title;     var titles = new array();     titles['item_name'] = "course title";     var outmessage = "";     invoiceonly_var = invoiceonly;      //alert(formname);      //alert(form.length);      var optional = new array();      // optional fields      optional['your_position'] = true;     optional['your_mobile'] = true;       for(i = 0; < form.length; i++)     {         element = form.elements[i];          type = element.type;         name = element.name;          //alert("name:"+name+" type:"+type);           if(type == "text")         {             fieldvalue = element.value;             params = params + name + "=" + fieldvalue + "&";              optional = optional[name];             if ( (fieldvalue == '' || fieldvalue == name ) && typeof(optional) == "undefined")             {                 title = gettitle(name,titles);                 badfields += "- " + title + "\n";             }                 //alert(params);         }         if(type == "textarea")         {             fieldvalue = element.value;             params = params + name + "=" + fieldvalue + "&";              optional = optional[name];             if ( (fieldvalue == '' || fieldvalue == name ) && typeof(optional) == "undefined")             {                 title = gettitle(name,titles);                 badfields += "- " + title + "\n";             }                 //alert(params);         }     }       if(badfields)     {         outmessage = "we unable proceed following \n";         outmessage += "required fields have not been completed:\n\n";         outmessage += badfields;          alert(outmessage);         return false;     }      //alert(params);      xmlhttp=getxmlhttpobject();      if (xmlhttp==null)     {         alert ("browser not support http request");         return;     }      url="/payment/ajax_email_me.php?";     if (invoiceonly_var == true)     {         params = params + "payment_method=invoice";     }     else     {         params = params + "payment_method=paypal&";     }     url=url+params;      //alert(url);      xmlhttp.onreadystatechange = finished;      xmlhttp.open("get",url,true)      xmlhttp.send(null)      return true; }  function finished()  {      if (xmlhttp.readystate==4 || xmlhttp.readystate=="complete")     {          //document.getelementbyid('search').innerhtml = xmlhttp.responsetext;         var response = xmlhttp.responsetext;          //alert(response);         if (invoiceonly_var == true)         {             alert("thank you. message has been sent.");         }         else         {             document.paymentform.submit();         }          document.body.innerhtml = document.body.innerhtml + "<span></span>";     } }  function getxmlhttpobject() {     var xmlhttp=null;      try     {         // firefox, opera 8.0+, safari         xmlhttp=new xmlhttprequest();         //  alert("mozilla");     }     catch (e)     {         //internet explorer         try         {             xmlhttp=new activexobject("msxml2.xmlhttp");             //  alert("ie");         }         catch (e)         {             xmlhttp=new activexobject("microsoft.xmlhttp");             //  alert("ieerror");         }     }      return xmlhttp; }  function selectval(sel) {     return sel.options[sel.selectedindex].value; }  function gettitle(name,titles) {     title = titles[name];     if (typeof(title) == "undefined")     {         title = name;         title = title.replace(/_/g," ");     }     return title; } 

did try adding "return false" end of onclick event don't want submit?

or can change type "button" "submit" - might trick.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -