从单选按钮PHP获取价值

时间:2018-04-26 11:57:08

标签: php forms radio-button

我正在制作一个表格,您可以通过单选按钮选择是否要匿名,但是,我无法让它工作。

这是我的HTML代码:

<input type="radio" name="anonym" value="ja" />Ja
<input type="radio" name="anonym" value="nei" checked/>Nei

PHP代码:

if (isset($_POST['snd_skjema'])) {
  // receive all input values from the form

  if($_POST['anonym'] == 'ja') {
    $anonym = $id;

  } elseif($_POST['anonym'] == 'nei') {
    $anonym = NULL;
  }

希望有人可以帮助我,谢谢:)

2 个答案:

答案 0 :(得分:0)

要查看女巫一,只需检查它是否包含数据。

像这样

if (isset($_POST['snd_skjema'])) {

  if($_POST['anonym']) {
     $anonym = $id;

  } else {
    $anonym = NULL;
 }

答案 1 :(得分:0)

在你的php中我没有看到你对你的id和匿名变量进行讽刺 试试这个

html

<input type="radio" name="anonym" value="ja" />Ja
<input type="radio" name="anonym" value="nei" checked/>Nei

PHP

//making sure reqquest method is post 

if ($_SERVER["REQUEST_METHOD"] == "POST"){
 $anonym_err = ""; $anonym = ""; $id = "";

/*verify that your anonym $_POST value is not empty first 
it could be the reason it does not work
if it is empty the anonym error variable picks an error*/

    if (empty($_POST["anonym"])) {
        $anonym_err = "choose an anonym type";
      } else {
        if($_POST["anonym"] == "ja"){
      $anonym = $id;
    }elseif($_POST["anonym"] == "nei"){
    $anonym = NULL;
    } 
  }
}