post 提交后,外部 php 文件中没有任何显示

Nothing displays in external php file after post submit

所以我有这个问题,在index.php中选择一个值并提交后,页面重定向到Payment.php但是没有显示任何内容,文件之间的连接有效,甚至验证了如果排除了 isset,PaymentType 的值将起作用,所以我猜问题出在 isset 函数上,但我自己无法修复它。

index.php

<?php
require_once('Payment.php');
 ?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>

        <form action="Payment.php" method="post">
            <label for="PaymentType">Please select a payment method.</label><br>
            <select  name="PaymentType">
                <option value="visa">Visa</option>
                <option value="paypall">PayPall</option>
                <option value="mastercard">MasterCard</option>
            </select><br><br>
            <button type="submit" value="submit">submit</button>
        </form>
    </body>
</html>

Payment.php

<?php

interface PaymentInterface{
    public function pay();
}

class Visa implements Paymentinterface{
    public function pay(){
        echo "Paid with Visa";}
}

Class Paypall implements Paymentinterface{
    public function pay(){
        echo "Paid with PayPall";}
}

Class MasterCard implements Paymentinterface{
    public function pay(){
        echo "Paid with MasterCard";}
}

Class Payment{

    public function processPayment(PaymentInterface $payment){
        $payment->pay();
    }
}

if(isset($_POST['submit'])){
    $option = $_POST['PaymentType'];

    if($option=='visa'){
        $method = new Visa();
        $payment = new Payment();
        $payment->processPayment($method);
    }
    elseif($option=='paypall'){
        $method = new PayPall();
        $payment = new Payment();
        $payment->processPayment($method);
    }
    elseif($option=='mastercard'){
        $method = new MasterCard();
        $payment = new Payment();
        $payment->processPayment($method);
    }
    else{
        echo "Hey,what are you doing there?";
    }
};

有些地方不太对:

  • a <label>s for 属性应该指向具有相同 id
  • 的元素
  • 您的 <submit> 按钮没有 name 属性,这就是 if(isset($_POST['submit'])){ 永远不会被触发的原因

将您的表单更改为:

<form action="Payment.php" method="post">
  <label for="PaymentType">Please select a payment method.</label><br>
  <select name="PaymentType" id="PaymentType">
    <option value="visa">Visa</option>
    <option value="paypall">PayPall</option>
    <option value="mastercard">MasterCard</option>
  </select><br><br>
  <button type="submit" value="submit" name="submitButton">submit</button>
</form>

在你的Payment.php改变

if(isset($_POST['submit'])){

if(isset($_POST['submitButton'])){

附带说明:该服务称为 "PayPal",而不是 "PayPall"