所以我遇到了这个问题,在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?";
}
};
有些事情不太对劲。
<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"