PHP 多个单选按钮

发布于 2024-08-09 02:55:17 字数 362 浏览 4 评论 0原文

我如何处理页面中的所有单选按钮?

<input type="radio" name="radio_1" value="some" />
<input type="radio" name="radio_1" value="some other" />

<input type="radio" name="radio_2" value="some" />
<input type="radio" name="radio_2" value="some other" />

该按钮将动态添加,因此我不知道单选按钮名称(以及按钮的数量)。有没有办法通过 for 循环或类似的东西来获取所有无线电值?谢谢

how can i process all radio buttons from the page?

<input type="radio" name="radio_1" value="some" />
<input type="radio" name="radio_1" value="some other" />

<input type="radio" name="radio_2" value="some" />
<input type="radio" name="radio_2" value="some other" />

this buttons will be added dynamically so i will not know the radio buttons name(and also the number of the buttons). Is there a way to get all radio values, with a for loop or something like this? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明媚如初 2024-08-16 02:55:17

使用 foreach 循环

<?php
foreach ( $_POST as $key => $val )
    echo "$key -> $val\n";
?>

$key 将是所选选项的名称,$val 是值。

Use a foreach loop

<?php
foreach ( $_POST as $key => $val )
    echo "$key -> $val\n";
?>

$key will be the name of the selected option and $val, well, the value.

难如初 2024-08-16 02:55:17

由于浏览器只会将您的所有输入更改为 HTTP 格式的表单数据,因此您将无法区分哪些数据来自单选按钮,哪些数据来自文本框或其他输入。

如果命名约定与您的示例相同,则循环直到找不到值:

<?
  for ($idx = 1; $idx <= 1000; $idx++) {
    if (isset($_REQUEST["radio_$idx"])) {
      // handle value
    }
  }
?>

编辑 或者,如果您的表单是动态生成的,您可以将其创建的单选按钮的数量编写为表单中的隐藏字段。

如果您能够更改正在生成的表单,则可以编写一个隐藏的输入,该输入提供您想要查找的所有单选按钮的列表。当您编写单选按钮时,只需创建一个以分号分隔的所有名称列表。完成后,将其写入隐藏输入。像这样的事情:

在源表单上:

<input type="hidden" name="radio_button_list" value="some_value;other_value" />

然后在您的处理程序中:

<?
  $list = explode(';', $_REQUEST['radio_button_list']);
  foreach ($list as $name) {
    $value = $_REQUEST[$name];
    // handle name & value
  }
?>

Since the browser will just change all your input to HTTP-formatted form data, you won't be able to tell what data is from a radio button versus a text box or other input.

If the naming convention is the same as your example, just loop until you don't find a value:

<?
  for ($idx = 1; $idx <= 1000; $idx++) {
    if (isset($_REQUEST["radio_$idx"])) {
      // handle value
    }
  }
?>

EDIT Alternatively, if your form is generated dynamically, you could write the number of radio buttons it created as a hidden field in the form.

If you are able to alter the form that is being generated, you could write a hidden input that provided a list of all the radio buttons that you want to look for. As you are writing the radio buttons, just make a semi-colon-separated list of all the names that you make. When you are done, write that to a hidden input. Something like this:

On the source form:

<input type="hidden" name="radio_button_list" value="some_value;other_value" />

Then in your handler:

<?
  $list = explode(';', $_REQUEST['radio_button_list']);
  foreach ($list as $name) {
    $value = $_REQUEST[$name];
    // handle name & value
  }
?>
与风相奔跑 2024-08-16 02:55:17

jheddings 的例子说明了一切。但是,您永远不会获得所有按钮的名称/值 - 只能获得每组中选定的按钮。如果您确实需要所有值,则必须使用Javascript。

jheddings' example says it all. However, you will never get the names / values of all buttons - just the selected one from each group. If you need literally all values, you will have to use Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文