如何使PayPal按钮作为提交按钮工作


How to make a PayPal button work as a submit button

我的表单有点小问题

我制作了一个表格,输入客户的姓名和学年,后面的PHP代码会将证书邮寄给雇主,最初表格连接到"提交"按钮,它起作用了。现在我必须放一个PayPal按钮而不是提交按钮,因为这将是订购代金券卡的一种方式。

PayPal按钮不会与PHP代码连接。这是我的代码:

        <?php 
if (isset($_REQUEST['submit'])) {
// Initialize error array.
  $errors = array();
  // Check for a proper First name
  if (!empty($_REQUEST['firstname'])) {
  $firstname = $_REQUEST['firstname'];
  $pattern = "/^[a-zA-Z0-9'_]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}
  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your First Name.';}
  
  // Check for a proper Last name
  if (!empty($_REQUEST['lastname'])) {
  $lastname = $_REQUEST['lastname'];
  $pattern = "/^[a-zA-Z0-9'_]{2,20}/";// This is a regular expression that checks if the name is valid characters
  if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}
  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
  } else {$errors[] = 'You forgot to enter your Last Name.';}
  
  //Check for a valid Email
  if (!empty($_REQUEST['Email'])) {
  $Email = $_REQUEST['Email'];
  $pattern = "/(.*?){1,10}/";
  if (preg_match($pattern,$Email)){ $Email = $_REQUEST['Email'];}
  else{ $errors[] = 'Your year can only be numbers and letters.';}
  } else {$errors[] = 'You forgot to enter your order.';}
  
  }
    //End of validation 
  if (isset($_REQUEST['submit'])) {
  if (empty($errors)) { 
  $from = "Order in from " . $firstname. ""; //Site name
  // Change this to your email address you want to form sent to
  $to = "scoildaracookthebooks@gmail.com"; 
  $subject = "Amanda! Someone bought a card!" . $firstname . "";
  
  $message = "Message from " . $firstname . " " . $lastname . " has just purchased a card, make sure to check the account before you give them a card!
  School year: " . $Email . " ";
  mail($to,$subject,$message,$from);
  }
}
?>
<?php 
  //Print Errors
  if (isset($_REQUEST['submit'])) {
  // Print any error messages. 
  if (!empty($errors)) { 
  echo '<hr /><h3>The following occurred:</h3><ul>'; 
  // Print each error. 
  foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
  echo '</ul><h3>Your order could not be sent due to input errors.</h3><hr />';}
   else{echo '<hr /><h3 align="center">Your order was sent to Amanda. Thank you!</h3><hr /><p>Below is the contact details that you sent out to us.</p>'; 
  echo "<u>Order  €20 voucher from " . $firstname . " " . $lastname . " </u><br /><b>School year: </b> ".$Email."";
  }
  }
//End of errors array
  ?>
    </div>
    </div>
      </div>
        <div class="clearfix visible-lg"></div>
      </div>
      <div class="container">
        <div class="jumbotron">
    <h2>Order now!</h2>
  <p>Fill out the form below.</p>
  <div class ="form-group">
  <form role ="form" action="" method="post">
  <label>First Name: <br />
  <input name="firstname" type="text" class="form-control" placeholder="- Enter First Name -" /><br /></label>
</div>
   <div class ="form-group">
  <label>Last Name: <br />
  <input name="lastname" type="text" class="form-control" placeholder="- Enter Last Name -" /><br /></label>
  </div>
   <div class ="form-group">
  <label>School year: <br />
  <input name="Email" type="text" class="form-control" placeholder="- Enter School year -" /><br /></label>
</div>
  
   <div class ="form-group">
    <!-- This is where the problem starts, the name="submit" won't connect with the PHP code-->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"  target="_blank">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="HEVRWPVT75BKE">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" name="submit" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</div>
 </div>
  </form>

看看PayPal Cart Upload方法。您将使用它,而不是现在使用的按钮/表单。

这种方法将允许你建立自己的单一表单,你可以将你想要的任何参数加上PayPal提供的变量(许多作为隐藏字段)组合起来。

您的表单可以提交给一个"处理器"脚本,该脚本可以对表单中您自己的数据执行任何需要的操作,然后还可以使用PayPal数据生成一个URL字符串,您可以将用户重定向到该字符串以进行PayPal支付。

如果你需要处理数据库的任何交易后处理、电子邮件通知等,你会想用IPN来做。