text: Q:
  
  different values for radio button if selected or not
  
  I'm working on a online exam website.
  when I want to define a question with its choices, I should choose the correct choice. so there is a radio button in front of each choice that if I choose it, It will be the correct choice.
  I want when I choose one of the radio buttons, the value of it would be '1' and if not, the value would be '0', so I can save them in database with foreach. that's my problem.
  Since the number of choices is not clear (from 2 to more) and it can be added dynamically, I can't set specific values for them.
  I used isset and foreach but I guess I didn't use them in correct way.
  I think it can be done by javascript or jquery but I don't know how.
  choice 1:
  <textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
  <label><input type="radio" name="correct[]">Correct</label>
  
  choice 2:
  <textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
  <label><input type="radio" name="correct[]">Correct</label>
  
  Other Choices ...
  
  <?php
  foreach ( $_POST['choice'] as $key => $value ){
  if (!empty($value)){
      foreach ($_POST['correct'] as $value2){ 
      if (isset($value2)) $choice_answer = '1'; else $choice_answer = '0';}
  
  mysqli_query($server,"INSERT INTO question_choices (question_id,exam_id,choice,answer) VALUES ('$question_id','$current_exam_id','$value','$choice_answer')");}}
  ?>
  
  A:
  
  So you have your choices being stored in an array, this is good.  However, you also have your correct indicator being stored in an array.  You don't need it in an array since there is only one correct choice.
  So change your correct[] to just correct and set the value incrementally for every choice.
  Ex:
  choice 1:
  <textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
  <label><input type="radio" name="correct" value=0>Correct</label>
  
  choice 2:
  <textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
  <label><input type="radio" name="correct" value=1>Correct</label>
  
  choice 3:
  <textarea class="form-control" name="choice[]" placeholder="choice text"></textarea>
  <label><input type="radio" name="correct" value=2>Correct</label>
  
  Since only selected radio buttons get posted, $_POST['correct'] will contain the index of the correct answer within the $_POST['choice'] array.
  Then you can do something like this to set your value accordingly before you send to the database:
  foreach ($_POST['choice'] as $i => $choice) {
      if (!empty($choice)) {
  
          if($_POST['correct'] == $i){
              $choice_answer = 1;
          } else {
              $choice_answer = 0;
          }
  
          mysqli_query($server, "INSERT INTO question_choices (question_id,exam_id,choice,answer) VALUES ('$question_id','$current_exam_id','$value','$choice_answer')");
      }
  }
  
  