Post 到 PHP 贝宝付款成功后的脚本

Post to PHP script after successful paypal payment

我有一份 html 俱乐部注册表格。然后我有一个我想调用的 php 脚本,它将向我发送一封包含注册信息的电子邮件。我需要做的是在我通过 paypal 成功付款后调用 PHP 脚本。在沙盒下,我成功地点击了 'onApprove' 回调。我只是想知道如何调用 PHP 文件和 post 表单中的数据。任何帮助,将不胜感激。或者如果有更好的方法,请告诉我。

HTML:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  background-color: black;
}

* {
  box-sizing: border-box;
}

/* Add padding to containers */
.container {
  padding: 16px;
  background-color: white;
}

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for the submit button */
.registerbtn {
  background-color: #000000;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

.registerbtn:hover {
  opacity: 1;
}

/* Add a blue text color to links */
a {
  color: dodgerblue;
}

/* Set a grey background color and center the text of the "sign in" section */
.signin {
  background-color: #f1f1f1;
  text-align: center;
}
</style>
</head>
<body>

<form method="post" name="emailregistration" action="form-to-email.php">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to register.</p>
    <hr>

    <label for="email"><b>Email*</b></label>
    <input type="text" placeholder="user@email.com" name="email" id="email" required>

    <label for="name"><b>Name (First Last)*</b></label>
    <input type="text" placeholder="John Smith" name="name" id="name" required>

    <label for="address"><b>Address*</b></label>
    <input type="text" placeholder="555 S Main, Prosper, TX 75078" name="address" id="address" required>

    <label for="phone"><b>Phone Number*</b></label>
    <input type="text" placeholder="555-555-5555" name="phone" id="phone" required>

    <label for="age"><b>Age*</b></label>
    <input type="text" placeholder="35" name="age" id="age" required>

    <br>
    <br>

    <label><b>Experience Level-I played in...*</b></label>
    <br>
    <br>
    <input type="radio" name="experience" id="neverPlayed" value="never" required>
    <label for="neverPlayed">I've never played organized football</label><br>
    <input type="radio" name="experience" id="middleSchool" value="middleSchool" required>
    <label for="middleSchool">Middle School</label><br>
    <input type="radio" name="experience" id="highSchool" value="highSchool" required>
    <label for="highSchool">High School</label><br>
    <input type="radio" name="experience" id="College" value="college" required>
    <label for="college">College</label><br>
    <input type="radio" name="experience" id="semiPro" value="semiPro" required>
    <label for="semiPro">Semi-Pro</label><br>
    <input type="radio" name="experience" id="professional" value="professional" required>
    <label for="professional">Professional</label><br>

    <br>
    <br>

    <label><b>Team Designation*</b></label>
    <br>
    <br>
    <input type="radio" name="team" id="captain" value="captain" required>
    <label for="captain">I wish to Captain a team (you will receive an additional email requesting more information)</label><br>
    <input type="radio" name="team" id="placed" value="placed" required>
    <label for="placed">I wish to be placed on a currently formed team (team name and captain's name required next)</label><br>
    <input type="radio" name="team" id="added" value="added" required>
    <label for="added">I wish to be added to a new team</label><br>

    <br>
    <br>

    <label for="formedteam"><b>If you answered "I wish to be placed on a currently formed team" above, please provide the team name and captain's name.</b></label>
    <input type="text" placeholder="Your answer" name="formedteam" id="formedteam">

    <label for="request"><b>Special Request (i.e. To be placed on the same team as another player)</b></label>
    <input type="text" placeholder="Your answer" name="request" id="request">

    <label for="emergency"><b>Emergency Contact Name/Phone Number/Relationship</b></label>
    <input type="text" placeholder="Your answer" name="emergency" id="emergency">

    <label for="emergency2"><b>2nd Emergency Contact Name/Phone Number/Relationship</b></label>
    <input type="text" placeholder="Your answer" name="emergency2" id="emergency2">

    <hr>
   

    <button type="submit" class="registerbtn">Register</button>
    <div>
    <script src="https://www.paypal.com/sdk/js?client-id=[key]">
    </script>
  
    <div id="paypal-button-container"></div>
  
    <script>
        paypal.Buttons({
            createOrder: function(data, actions) {
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: '69.99'
                        }
                    }]
                });
            },
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                  $.post("form-to-email.php", {
                      value: document.forms[emailregistration]
                  }, function(data) {
                    window.location.href = 'index';
                    alert("Success");
                  });

                    // document.forms["emailregistration"].submit(function() {
                    //   alert("Success");
                    // });
                    
                });
            }
        }).render('#paypal-button-container');
    </script>
    </div>
  </div>
  
  
  
</form>

</body>
</html>

PHP 脚本:

<?php
if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}
$email = $_POST['email'];
$name = $_POST['name'];

if(empty($name))
{
    echo "Name and email are mandatory!";
}

$email_from = "email@email.com";
$email_subject = "New Registration";
$email_body = "There is a new registration from $name.\n email- $email";
$to = "email@email.com";
$headers = "From: $email_from \r\n";

mail($to, $email_subject, $email_body, $headers);
?>

为了在付款成功后调用PHP文件,您可以使用jQuery的.post()方法轻松调用该文件。

下面是 post 调用在您的 JavaScript 代码中的外观示例。一旦 JavaScript 被调用,使用这个将成功调用你的 PHP 文件,你可以使用 data 有效载荷将 return 来自 PHP 文件的数据返回到原始页面。

  $.post("yourfile.php",
  {
    value1: "Red",
    value2: "Blue"
  },
  function(data){
    alert("Data: " + data);
  });

为了更好地理解jQuery.post()方法,您可以在这里查看文档:

https://api.jquery.com/jQuery.post/