PHP:无法从单选按钮POST中检索数据

时间:2017-10-09 02:04:42

标签: php post radio-button

我在从单选按钮检索数据时遇到问题。我可以从其他html标签中检索其他数据,但在单选按钮中我不能。这是为什么?你能救我吗?

我的HTML代码在这里:

<form enctype="multipart/form-data" name="markpayment" method="post" action="test.app">
  <input type="hidden" name="action" value="mark_payment">
  <input type="hidden" name="client-id" value="{$details[row]->id}">
  <input type="hidden" name="invoice-id" value="{$details[row]->invoice_id}">
  <input type="hidden" name="period" value="{$details[row]->period}">
  <table width="100%" border="0" style="padding-top: 10px;">
    <tr align="center">
      <td class="radiobut">
        <input type="radio" id="choice1" name="payment_status" value="paid"> Paid</td>
      <td class="radiobut">
        <input type="radio" id="choice2" name="payment_status" value="declined"> Declined</td>
    </tr>
    <tr align="center">
      <td colspan="2">
        <div class="paddingRow3">
          <select class="selectbut" id="payments" name="payment-method">
            <option value="Cheque">Cheque</option>
            <option value="Deposit">Deposit</option>
            <option value="Cash">Cash</option>
          </select>
        </div>
      </td>
    </tr>
    <tr align="right">
      <td colspan="2">
        <div class="paddingRow3">
          <a href="javascript:void(0);" class="update" onclick="document.markpayment.submit()">Update</a>
        </div>
      </td>
    </tr>
  </table>
</form>  

我的PHP脚本在这里:

if($_REQUEST['action'] == 'mark_payment'){
    echo '<pre>';
    print_r($_REQUEST);
    exit;
}

我的输出将是这样的:

Array
(
    [NONCE] => f305790c4d8b060121b99fe84a8fdf1a62321b3b06b9097caa8439e2f9c5bae7
    [action] => mark_payment
    [client-id] => 2699422
    [invoice-id] => 13008351
    [period] => 11
    [payment-method] => Cheque
)

1 个答案:

答案 0 :(得分:1)

对于您的两个选择,您有namepayment_status个属性。它们需要不同,或者只有最后一个输入元素(#choice2)可用。

我在以下示例中使用payment_status_paidpayment_status_declined,但您也可以使用方括号表示name="payment_status[]"来生成数组 POST数据。

<form enctype="multipart/form-data" name="markpayment" method="post" action="test.app">
  <input type="hidden" name="action" value="mark_payment">
  <input type="hidden" name="client-id" value="{$details[row]->id}">
  <input type="hidden" name="invoice-id" value="{$details[row]->invoice_id}">
  <input type="hidden" name="period" value="{$details[row]->period}">
  <table width="100%" border="0" style="padding-top: 10px;">
    <tr align="center">
      <td class="radiobut">
        <input type="radio" id="choice1" name="payment_status_paid" value="paid"> Paid</td>
      <td class="radiobut">
        <input type="radio" id="choice2" name="payment_status_declined" value="declined"> Declined</td>
    </tr>
    <tr align="center">
      <td colspan="2">
        <div class="paddingRow3">
          <select class="selectbut" id="payments" name="payment-method">
            <option value="Cheque">Cheque</option>
            <option value="Deposit">Deposit</option>
            <option value="Cash">Cash</option>
          </select>
        </div>
      </td>
    </tr>
    <tr align="right">
      <td colspan="2">
        <div class="paddingRow3">
          <a href="javascript:void(0);" class="update" onclick="document.markpayment.submit()">Update</a>
        </div>
      </td>
    </tr>
  </table>
</form>

要查找选择的那个,请使用条件:

if($_REQUEST['action'] == 'mark_payment'){
    echo '<pre>';
    print_r($_REQUEST);
    if ($_POST['payment_status_paid']) {
      // Payment was successful
    }
    else {
      // Payment was declined
    }
    exit;
}

希望这有帮助!