php 在php中接收单选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5167596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:01:04  来源:igfitidea点击:

receiving radio box value in php

phpradio-button

提问by Jay

I have 2 following radio boxes in a form,

我在表格中有 2 个以下单选框,

<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No
  1. How can i recieve the value of the radio button once the form is posted (in PHP)
  2. Once it is posted on the same page, how can I rememberthe selected radio button and keep that checked? Thanks.
  1. 表单发布后,我如何接收单选按钮的值(在 PHP 中)
  2. 一旦它发布在同一页面上,我如何记住所选的单选按钮并保持选中状态?谢谢。

回答by Czechnology

1) The value of the radio button is saved in $_POSTonly ifany of the choices was selected.

1) 单选按钮的值$_POST在选择了任何选项时才保存。

if (isset($_POST['radio']))   // if ANY of the options was checked
  echo $_POST['radio'];    // echo the choice
else
  echo "nothing was selected.";


2) Just check for the value and add checked='checked'if it matches.

2)只需检查值并添加checked='checked'是否匹配。

<input type="radio" name="radio" value="yes" class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] == 'yes'): ?>checked='checked'<?php endif; ?> /> Yes
<input type="radio" name="radio" value="no"  class="radio" <?php if (isset($_POST['radio']) && $_POST['radio'] ==  'no'): ?>checked='checked'<?php endif; ?> /> No

回答by shanmugavel-php

<input type="radio" name="radio" value="yes" class="radio" /> Yes
<input type="radio" name="radio" value="no" class="radio" /> No

 u get radio value using $_POST['radio'];

simple bro,

简单的兄弟

<input type="radio" name="radio" <?php if($_POST['radio']=="yes") echo "checked";?> value="yes" class="radio" /> Yes

u have to identify radio box by value man

你必须通过价值人来识别收音机盒

回答by Quentin

How can i recieve the value of the radio button once the form is posted (in PHP)

表单发布后,我如何接收单选按钮的值(在 PHP 中)

$_POST['radio']

Once it is posted on the same page, how can I remember the selected radio button and keep that checked?

一旦它发布在同一页面上,我如何记住所选的单选按钮并保持选中状态?

Add a checkedattribute ifthe value is equal to $_POST['radio'].

添加值等于的checked属性。if$_POST['radio']

回答by diEcho

1) u will receive only that radio box value via POSTwhich is checked

1)您将仅通过已选中的POST接收该单选框值

    $radio_value=$_POST['radio'];

2)

2)

<input type="radio" name="radio" value="yes" class="radio" 
   <?php echo ($radio_value == 'yes') ? 'checked="checked"' : ''; ?>/> Yes
<input type="radio" name="radio" value="no" class="radio" 
   <?php echo ($radio_value == 'no') ? 'checked="checked"' : ''; ?>/> No