我如何知道 php 中选择了哪个单选按钮?

How can I know which radio button is selected in php?

这是我在html的广播表格:

<form action="form2.php" method="POST">
<label><input type="radio" name="select" value="insert"> Insert </label>
<label><input type="radio" name="select" value="remove"> Remove </label>
</form>

如何检查在 php 文件(与数据库连接)中选择了 2 个无线电中的哪一个。 例如,如果选择插入则添加新成员。如果选择删除,则删除成员。

$select = $_REQUEST['select'];
echo $select;

它 returns “开启” 不是 insert/Insert 也不是 remove/Remove

这个很简单。只需检查您的 post 数据的值。

<?php
if(isset($_REQUEST['select'])){
  switch ($_REQUEST['select']) {
      case 'insert':
        // Insert new

        break;
      
      case 'remove':
        // Remove
        break;
    }  
}

为您的收音机输入添加 value 属性:

<form action="form2.php" method="POST">
<label><input type="radio" name="select" value="insert"> Insert</label>
<label><input type="radio" name="select" value="remove"> Remove</label>
<button>submit</button>
</form>

我还添加了一个提交按钮。

如果form2.php包含

<?php
echo $_REQUEST['select'];

<?php
echo $_POST['select'];

提交表单后会打印出insertremove 取决于您选择的收音机。

有关无线电输入的更多信息:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio