Domanda

I have for example 1 questions and 2 option field in which each of the field would have a radio button to indicate whether that option is the correct answer or not.

echo"<p>Enter Question:<input type=\"text\" name=\"question[]\" size=\"40\" maxLength=\"1000\" >";

Option 1:<input type=\"text\" name=\"option[$num][]\" size=\"40\" maxLength=\"100\"> <input type=\"radio\" name=\"weight[$question_num][]\" data-role=\"none\" style=\"text-align:justify\">Correct answer</p></br>";

Option 2:<input type=\"text\" name=\"option[$num][]\" size=\"40\" maxLength=\"100\"> <input type=\"radio\" name=\"weight[$question_num][]\" data-role=\"none\" style=\"text-align:justify\">Correct answer</p></br>";

Both of the radio button is in a group so it works prefectly fine. However, in the back end server:

if (isset($_POST['weight'])) {
         print_r($_POST['weight']);
         foreach ($_POST['weight'] as $key => $weighteach) {
             foreach ($weighteach as $key2 => $value) {
                 echo"<br/>";
                 $key2= $key2+1;
                 echo $key.':'.$key2;

                 echo"<br/>";

       }
    }
}

I'm trying to echo Question Number: Option Number of the radio button I selected before I store into my DB. But even if I selected Option2, the output will be 1:1 instead of 1:2. Please help me to the best that you can. Will greatly appreciate it.

È stato utile?

Soluzione

You have not given your radio buttons values:

<input type=\"radio\" name=\"weight[$question_num][]\" data-role=\"none\" style=\"text-align:justify\">

should be something like:

<input type=\"radio\" name=\"weight[$question_num][]\" value='Value of radio button 1' data-role=\"none\" style=\"text-align:justify\">

Now you can get the value of the selected radio button in php.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top