簡體   English   中英

將PHP插入頁面

[英]Inserting PHP into a page

這可能是一個非常基本的PHP問題,但是對於PHP來說,我是個完全菜鳥,因此我將根據我的特定問題提出這個問題。

我正在使用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)?

這不是最干凈的方法,而是執行此操作的基本方法。

<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>
<div>
      <?php 
         require_once('path/to/phpFileWithCodeToBeExecuted.php')
      ?>
</div>

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

我要在這里四肢走動,告訴你要怎么做。 但首先

前言

您可以使用php進行模板制作。 您的站點必須具有可遵循的模板方案。 這些模板通常是帶有邏輯標記的php標記的html或php“ stuff”。

腳步

  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. 模板中的原始文本將與echoprintdump以及引發的任何錯誤一起發送到STDOUT

附錄

這只是一種實現方法的示例,還有更好的方法,這里已經給出的一些答案也很好

任何問題???? 請問^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM