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

时间:2015-02-08 22:57:24

标签: php html forms paypal

我对表单有轻微问题,

我已经制作了一个输入客户姓名和学年的表格,其背后的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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

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

该方法将允许您构建自己的单个表单,并且您可以将您想要的任何参数与variables that PayPal provides(许多作为隐藏字段)组合在一起。

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

如果您需要处理数据库的任何事后处理,电子邮件通知等,您需要使用IPN进行处理。