Search code examples
phpformsisset

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

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

Solution

  • Some things not quite right here:

    • a <label>s for attribute should point to an element with the same id
    • your <submit> button doesn't have a name attribute, that's why if(isset($_POST['submit'])){ never gets triggered

    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

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

    to

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

    On a side note: the service is called "PayPal", not "PayPall"