将PHP插入页面

时间:2013-11-29 12:40:17

标签: php

这可能是一个非常基本的PHP问题,但是当涉及到PHP时我是完全的noob,所以我会根据我的具体问题来问这个问题。

我正在使用Stripe Checkout和PHP在网站上进行信用卡付款。它的设置和工作正常。但是,我现在需要做的是将Stripe返回的数据(成功付款或卡片下降等交易错误)提供给符合我网站其余部分设计的页面。

例如,目前,我有以下PHP用于向卡收费,报告成功或捕获并报告错误:

try{

  $customer = Stripe_Customer::create(array(
      'email' => $email,
      'card'  => $token
  ));

  $charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $grandtotal,
      'currency' => 'usd',
      'description' => $email
  ));

  echo '<h1>Successfully charged $50.00!</h1>';
}catch(Stripe_CardError $e) {
  // Since it's a decline, Stripe_CardError will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
}
?>

这已经从示例和API文档中组合在一起并且工作正常,但显然它只是在空白页上打印结果。

如何获取相关信息(实际上是消息)并将其插入“正确”的网站页面(网站的其余部分只是静态HTML和CSS)并具有适当的样式等?

3 个答案:

答案 0 :(得分:0)

这不是最干净的方式,但却是如何做到这一点的基本方法。

<html>
  <body>
    Some text<br />
    <?php
      try
      {
        $customer = Stripe_Customer::create(array(
            'email' => $email,
            'card'  => $token
        ));

        $charge = Stripe_Charge::create(array(
            'customer' => $customer->id,
            'amount'   => $grandtotal,
            'currency' => 'usd',
            'description' => $email
        ));

        echo '<h1 class="resultsuccess">Successfully charged $50.00!</h1>';
      } catch(Stripe_CardError $e) {
        // Since it's a decline, Stripe_CardError will be caught
        $body = $e->getJsonBody();
        $err  = $body['error'];

        echo '<div class="resultfail">';
        echo 'Status is:' . $e->getHttpStatus() . "<br/>";
        echo 'Type is:' . $err['type'] . "<br/>";
        echo 'Code is:' . $err['code'] . "<br/>";
        // param is '' in this case
        echo 'Param is:' . $err['param'] . "<br/>";
        echo 'Message is:' . $err['message'] . "<br/>";
        echo '</div>';
      }
    ?>
    <br />
    Other text
  </body>
</html>

答案 1 :(得分:0)

<div>
      <?php 
         require_once('path/to/phpFileWithCodeToBeExecuted.php')
      ?>
</div>

http://php.net/manual/en/function.require-once.php

答案 2 :(得分:0)

我会在这里走出去,告诉你一步一步做什么。但首先

前导

您可以使用php进行模板化。您的网站必须具有可以遵循的模板方案。这些模板通常是带有围绕逻辑或php“stuff”的php标签的html。

步骤

  1. 创建一个模板,输出最终文档供用户查看。保留占位符以放置动态数据

    <!-- a paragraph that will be populated through php -->
    <p><?php echo $a_name_that_should_make_sense_to_you; ?></p>
    
  2. 完成模板后,您的逻辑应准备好环境。作为一个例子

    <?php // this is a php scriptfile that executes logic
    
    // your logic goes here
    // some of the variables from logic have to reach the template
    $a_name_that_should_make_sense_to_you = 'this is a short paragraph';
    
  3. 包含模板,使其在您的逻辑旁边“放置”,从而授予其访问所需变量的权限。

    include 'your/wonderfully/marvelous/template.php';
    
  4. 模板中的原始文本将与echo以及printdump以及任何错误抛出的内容一起发送到STDOUT

  5. 附录

    这只是一种方法的例子,有更好的方法,这里已经给出的一些答案也很好

    有问题????请问^^

相关问题