php中未定义的索引错误以及如何解决它

时间:2018-01-26 11:21:52

标签: php

我是PHP的初学者

我在下面创建了PHP文件。

它给出了注意:未定义的索引:此页面中的付款。

我一直收到这条消息,有人可以帮我解决吗?我没注意到我的线路有什么问题。

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

如何解决此错误?

3 个答案:

答案 0 :(得分:2)

抱怨您正在访问payment内的$_POST

试试这个:

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

答案 1 :(得分:1)

错误发生在第一页打开时,因为此时用户尚未按下表单的提交按钮。

所以,请使用以下代码:

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

答案 2 :(得分:1)

您检查了$ _POST ['finish']是否存在,但在尝试引用之前没有检查$ _POST ['payment']是否存在。
所以最初$ _POST数组不包含项'付款' - 因此无法找到索引'payment'(在$ _POST数组中)。