开发者

Email sending using jquery not working :PHP

As far as I can tell, this script is working fine in Chrome and IE. It's not working right in Firefox. (Not a promise that everything is working perfectly -- occasionally an email hasn't gone through, but we haven't been able to recreate that error.)

EDIT: It now seems that in Chrome and Firefox, the email isn't sent on the first try, but if I hit the back button from PayPal and submit again, it is s开发者_如何学JAVAent.

I thought the problem might be AdBlock Plus disabling the scripts somehow. Disabled ABP on the page, and it sent ... but after refreshing the page, and trying to send again, it was back to not working.

This registration form is supposed to send an email containing a bunch of form data, alter the appropriate inputs to prepare for submission to PayPal with what the user registered for, then redirect the user to PayPal to process payment separately. The redirect to PayPal is always happening perfectly, with the appropriate items in cart.

This code is in the head of the HTML file. (It used to use a minified jQuery file of my own, so in searching for errors I tried linking to this one -- didn't fix the problem.)

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script type="text/javascript" src="[myscripts].js"></script>

The form begins:

<form id="[myformname]" name="[myformname]" method="post" action="">

[myscripts].js contains this:

$(function() {  
  $('.error').hide();  
  $(".button").click(function() {

    // many lines pulling input from form, generally of the type:
    var firstname = $("input#firstname").val();
    // as well as 
    var subscribe = $("input#subscribe").is(':checked');

    // simple validation code for now, here is a generalized version
    if (firstname == ''){
       $('p#errors').html("Error: First name is required.");
       $("p#errors").show();
       return false;
    }
    else if (lastname == ''){
       $('p#errors').html("Error: Last name is required.");
       $("p#errors").show();
       return false;    
    }
    else {
       $("p#errors").hide();
    }


    fixFields();   // this does the adjustment for PayPal
                   // checked checkboxes -> item_name_1, amount_1, etc.

    // in the real code, submitString is very long
    var submitString = 'firstname='+firstname+'&lastname='+lastname+'&email='+email+'&subscribe='+subscribe; // etc.

    $.ajax({  
      type: "POST",  
      url: "register.php",  
      data: submitString,  
      success: function() {
        return false;
      }  
    });

    submitForm(); // resets action to PayPal link, resubmits

  });  
});  

In case it is relevant, here is the submitForm method, defined earlier in [myscripts].js:

function submitForm(){
  var form_url = $("#[myformname]").attr("action"); 
  //changing the action
  $("#[myformname]").attr("action","https://www.paypal.com/cgi-bin/webscr");
  //submit the form
  $("#[myformname]").submit();
  return true;
}

Here is register.php:

<?php
 $to = "[example1]@gmail.com,[example2]@gmail.com";
 $subject = "New Registration";
 $headers = "From: webmaster@[mysite.com]";

 // shortened for readability's sake
 $name = $_POST['firstname']." ".$_POST['lastname'];

 $body = "INFORMATION\n".$name."\n".$_POST['email'].", Subscribe: ".$_POST['subscribe'];

 $body = $body."\nSELECTIONS\n";
 if ( $_POST['option1']=="true" ) {
    $body = $body."Option 1 Name\n     Details Line 1\n     Details Line 2\n";
 }
 if ( $_POST['option2']=="true" ) {
    $body = $body."Option 2 Name\n     Details Line 1\n     Details Line 2\n";
 }

 mail($to, $subject, $body, $headers);

?>

Sorry if this is a ridiculously long post. I'm a programming enthusiast and very new to many of the languages I'm using here, so I wanted to be as clear as possible. Thanks in advance for any suggestions.


Your probably not giving it enough of a chance to send the form via AJAX before you submit it to paypal, try moving the submitForm() function inside of the success AJAX function.


This is a JavaScript error. Not a PHP Error. Install firebug on Firefox, enable the console, persist all errors and try to run the script. You will see problems.

Also, JavaScript conditions are slightly different to PHP. if(blah == '') is bad coding.

if you are wanting length it should be: if(blah.length >= 1). I will look at your javascript and see if anything stands out..

But, the answer is to debug in your firefox console.

Okay, here is your problem... You are submitting the form, the trying to move on before you receive the response in your jquery ajax method.. You need to click the button, it triggers off the ajax call, then does nothing until completion. I will write a script for you and post shortly.

Solution:

$.ajax({  
      type: "POST",  
      url: "register.php",  
      data: submitString,  
      success: function() {
        submitForm();
      }, error: function() {
         //something went wrong, use console.log here if you know what that is.
      }  
 });

    //submitForm(); // this was firing before ajax was done.


try checking for if(isset($_POST["whatever"]) AND $_POST["whatever"]). Sending the string "true" is a browser convention.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜