从单选按钮PHP获取价值

I am making a form where you can choose whether you wanna be anonymous or not via a radio button, however, I cannot get it to work.

This is my HTML code:

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

PHP code:

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

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

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

Hope someone can help me, thanks :)

To see witch one is selected just check if it holds data.

like this

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

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

  } else {
    $anonym = NULL;
 }

in your php i don't see you inicializing your id and anonym variables try this below

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;
    } 
  }
}