1

I am a beginner in PHP

I have created the PHP file below.

It gives Notice: Undefined index: payment in this page.

I keep getting this message can anyone help me out if possible please? I don't notice anything wrong with my line.

<form method="post" enctype="multipart/form-data">
  <div class="col-sm-10 col-sm-offset-1">
    <div class="col-sm-3">
      <div class="choice" data-toggle="wizard-radio" rel="tooltip" 
        title="" data-original-title="Select Any One    Package">
        <input type="radio" name="payment" value="12permonth">
        <div class="icon">
          <i class="fa fa-circle" aria-hidden="true"></i>
        </div>
        <h6>$12 Per Month</h6>
      </div>
    </div>

    <div class="col-sm-3">
      <div class="choice" data-toggle="wizard-radio" rel="tooltip" 
        title="" data-original-title="Select Any One    Package">
        <input type="radio" name="payment" value="30peryear">
        <div class="icon">
          <i class="fa fa-circle" aria-hidden="true"></i>
        </div>
        <h6>$30 Per Year</h6>
      </div>
    </div>

    <div class="col-sm-3">
      <div class="choice" data-toggle="wizard-radio" rel="tooltip" 
        title="" data-original-title="Select Any One    Package">
        <input type="radio" name="payment" value="39perfiveyear">
        <div class="icon">
          <i class="fa fa-circle" aria-hidden="true"></i>
        </div>
        <h6>$39 Per 5Year</h6>
      </div>
    </div>

    <div class="col-sm-3">
      <div class="choice" data-toggle="wizard-radio" rel="tooltip" 
        title="" data-original-title="Select Any One    Package">
        <input type="radio" name="payment" value="skippayment">
        <div class="icon">
          <i class="fa fa-circle" aria-hidden="true"></i>
        </div>
        <h6>Skip Payment</h6>
      </div>
    </div>
    <input type='submit' class='btn btn-finish btn-fill btn-success 
      btn-wd' name='finish' value='Finish' />
</form>
<?php

include('conn.php');

if (isset($_POST['finish']))
{
  $payment = mysqli_real_escape_string($conn, $_POST["payment"]);
  if ($_POST['payment'] == "skippayment")
  {
    echo "payment skipped";
  }
}
?>

How to fix this error?

Pierre François
  • 5,850
  • 1
  • 17
  • 38
Archi Patel
  • 65
  • 2
  • 14

3 Answers3

2

Its complaining that you are accessing payment inside $_POST.

Try this:

<?php

include('conn.php');

if (isset($_POST['finish']) && isset($_POST['payment'])) {

    $payment = mysqli_real_escape_string($conn, $_POST['payment']);

    if ($_POST['payment'] == 'skippayment') {
        echo 'payment skipped';
    }
}
Friedrich Roell
  • 437
  • 1
  • 5
  • 14
1

The error occurs at the first page opening because at that point the user haven't already pressed the form's submit button.

So, use this code:

if(!empty($_POST['finish']))
{
    if(isset($_POST['payment']))
    {
        $payment = mysqli_real_escape_string($conn,$_POST["payment"]);

        if($_POST['payment'] == "skippayment")
        {
            echo "payment skipped";
        }
    }
    else echo "payment empty";
}
user2342558
  • 5,567
  • 5
  • 33
  • 54
1

You checked if $_POST['finish'] exists, but did not check if $_POST['payment'] exists before trying to reference it.
So initially the $_POST array does not contain the item 'payment' - hence cannot find the index 'payment' (in the $_POST array).

GDB
  • 37
  • 4