带有验证和 Paypal 的 PHP 联系表

Posted

技术标签:

【中文标题】带有验证和 Paypal 的 PHP 联系表【英文标题】:PHP Contact Form w/ verification & Paypal 【发布时间】:2017-06-10 01:06:42 【问题描述】:

目标是让客户填写表格

然后点击提交,将发送一封电子邮件

然后会出现一条消息

然后重定向到一个简单的贝宝付款,这一切都是隐藏的

我无法让页面打开到贝宝

<? php

// configure
$name = $_POST['name'];
$from = 'contact <demo@domain.com>';
$sendTo = 'NEWCONTACT<new@gmail.com>';
$subject = 'New contact';
$fields = array('name' => 'Name', 'email' => 'Email', 'residence' => 'Residence', 'qualification' => 'Qualification', 'marital' => 'Marital', 'children' => 'Children', 'income' => 'Income', 'asset' => 'Asset', 'phone' => 'phone', 'nationality' => 'Nationality', 'interested' => 'interested', 'occupation' => 'Occupation', 'soccupation' => 'Spouse Occupation', 'SpouseEducationalQualification' => 'SpouseEducationalQualification', 'history' => 'History', 'source' => 'Source', 'comments' => 'Comments'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you '.$name.
', I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// let's do the sending

try 
  foreach($_POST as $key => $value) 
    if (isset($fields[$key])) 
      $emailText. = "<pre>".
      "$fields[$key]: $value".
      "</pre>";
    
  

  $headers = array('Content-Type: text/html; charset="UTF-8";',
    'From: '.$from,
    'Reply-To: '.$from,
    'Return-Path: '.$from,
  );

  mail($sendTo, $subject, $emailText, implode("\n", $headers));

  $responseArray = array('type' => 'success', 'message' => $okMessage);
 catch (\Exception $e) 
  $responseArray = array('type' => 'danger', 'message' => $errorMessage);


if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
  $encoded = json_encode($responseArray);

  header('Content-Type: application/json');

  echo $encoded;
 else 
  echo $responseArray['message'];


$query_data = array(
    'amount' => '900',
    'business' => 'uaetousavisa@gmail.com',
    'cmd' => '_xclick',
    'currency_code' => 'USD',
    'item_number' => $_POST['item_number'],
    'item_name' => $_POST['item_name']
);

// redirect to PayPal
header('Location: https://www.paypal.com/?'.http_build_query($query_data));
<form id="contact-form" method="post" action="contact.php" role="form">

  <div class="col-md-6">

    <div class="form-group">
      <input class="form-control" id="form_name" name="name" type="name" placeholder="Your Full Name" required="required" data-error="Firstname is required.">
      <div class="help-block with-errors"></div>
    </div>

    <div class="form-group">
      <input class="form-control" id="form_email" name="email" type="email" placeholder="Your email" required="required" data-error="Email is required.">
      <div class="help-block with-errors"></div>
    </div>

    <div class="form-group">
      <input class="form-control" id="income" type="text" name="income" placeholder="Net Monthly Income">
    </div>

  </div>
  <div class="col-md-12">
    <button type="submit" class="btn btn-default more-get" name="buy">Submit</button>
    <div class="messages"></div>


</form>

和php

【问题讨论】:

【参考方案1】:

您在 HTTP 标头中发送混合指令。

虽然您可以发送正文数据和重定向标头,但这毫无意义,因为重定向将被跟踪。

但真正的问题是您在设置重定向标头(header: location....');)之前发送数据(echo ...) - 您必须在输出之前发送所有标头。

如果您真的想按此顺序回显数据,请按照此处的 hack 操作:Interview Question: Can we have an echo before header?

【讨论】:

以上是关于带有验证和 Paypal 的 PHP 联系表的主要内容,如果未能解决你的问题,请参考以下文章

PHP 验证 Paypal webhook 签名

如何在 PHP 中验证 Paypal webhook 签名?

需要一些帮助来添加一些验证码联系表

带有透明重定向的 PayPal PayFlow Pro - 用户身份验证失败问题/文档

带有 paypal api 的客户端无效,使用 HTTPoison.post 的客户端身份验证失败!/3

PayPal PHP SDK:由于身份验证凭据无效或缺少授权标头,身份验证失败