简体   繁体   English

提交后外部 php 文件中不显示任何内容

[英]Nothing displays in external php file after post submit

So i have this problem, after selecting a value and submiting in index.php, the page redirects to Payment.php but nothing is displayed, the connection between the files works, and even the verification of the value from PaymentType works if the isset is excluded, so the problem i guess is from the isset function, but i could not fix it myself.所以我遇到了这个问题,在选择一个值并在 index.php 中提交后,页面重定向到 Payment.php 但没有显示任何内容,文件之间的连接有效,如果 isset 是,即使来自 PaymentType 的值验证也有效排除在外,所以我猜这个问题来自 isset function,但我自己无法修复。

index.php 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

<?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?";
    }
};

Some things not quite right here:有些事情不太对劲:

  • a <label> s for attribute should point to an element with the same id属性for <label>应该指向具有相同id的元素
  • your <submit> button doesn't have a name attribute, that's why if(isset($_POST['submit'])){ never gets triggered您的<submit>按钮没有name属性,这就是为什么if(isset($_POST['submit'])){永远不会被触发

Change your form to this:将您的表单更改为:

<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>

In your Payment.php change在您的Payment.php更改

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

to

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

On a side note: the service is called "PayPal", not "PayPall"附带说明:该服务称为“PayPal”,而不是“PayPall”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM