【问题标题】:PHP: cannot retrieve data from radio button POSTPHP:无法从单选按钮 POST 中检索数据
【发布时间】:2018-03-20 03:48:33
【问题描述】:

我在从单选按钮检索数据时遇到问题。我可以从其他 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
)

【问题讨论】:

  • 您是否单击/选择了任何单选选项?
  • @Akintunde 我做了,但没用
  • @Akintunde 的评论是正确的。为了测试,您可以为其中一个无线电设置 checked="true" 并查看该值是否到达服务器?

标签: php post radio-button


【解决方案1】:

您有重复的name 属性payment_status 用于您的两个选择。它们必须不同,否则只有最后一个输入元素 (#choice2) 可用。

在下面的示例中,我使用了payment_status_paid 和payment_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;
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2016-09-06
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多