0

I want to get the values that belong to the answers of a test. There could be any number of questions. A question has the HTML:

<form method='post' action='calificar.php'>

<div class='pruebaAlumno'>

    <h5>Pregunta 3</h5>
    <h3>¿Que factores definen la capacidad de una persona?</h3>

    <input type='checkbox' name='respuestaAlumno[]' 
                      value='Voluntad'> Voluntad <br>
    <input type='checkbox' name='respuestaAlumno[]' 
                      value='Cultura'> Cultura <br>
    <input type='checkbox' name='respuestaAlumno[]' 
                      value='Hábitos'> Hábitos <br>
    <input type='checkbox' name='respuestaAlumno[]' 
                      value='Habilidad'> Habilidad <br>
    <input type='checkbox' name='respuestaAlumno[]' 
                      value='Perfil'> Perfil <br>
    <input type='checkbox' name='respuestaAlumno[]' 
                      value='Dones'> Dones <br>

    <input type='hidden' name='idPregunta' value='41'>
    <input type='hidden' name='tipo' value='C'>
             </fieldset>

            </div>  <div class='calificar'>
                    <input type='submit' name='calificar' value='enviar' >
                </div>
            </form>

I want to get

  1. Answer (respuestaAlumno)
  2. questionID (idPregunta)
  3. type (tipo)

from each question.

Then I want to save the data in an array.

A test can have as many questions as the teacher wants. The form has one submit button.

I´ve tried to get the data in different ways. Using JS DOM. Using POST. I´ve found some PHP DOM tutorials but them all teach how to modify an static HTML.
Please help me to get the submitted data from each fieldset. Thanks in advance.

2 Answers2

0

You could format your form names like this.

First question

name="respuestaAlumno[0][]"
name="respuestaAlumno[0][]"
...
name="idPregunta[0]"
name="tipo[0]"

Second question

name="respuestaAlumno[1][]"
name="respuestaAlumno[1][]"
...
name="idPregunta[1]"
name="tipo[1]"

You would end up with three arrays in your $_POST.

Vincent Poirier
  • 4,298
  • 5
  • 21
  • 25
  • It worked for me. I also read http://stackoverflow.com/questions/2433727/submitting-a-multidimensional-array-via-post-with-php DOM was not necessary. Thanks – Arturo Burbano Jun 04 '15 at 20:40
0

Try this

 <?php 
 if(isset($_POST['calificar'])
 {

 $respuestaAlumno=$_POST['respuestaAlumno'];
 //you need respuestaAlumno array to single string
 echo $data_to_string=implode(",",$respuestaAlumno);
 $count_of_respuestaAlumno=count($_POST['respuestaAlumno']);
 for($i=0;$i<=$count_of_respuestaAlumno-1;$i++)
 {
 echo $data=$respuestaAlumno[$i]."/n";
 }
 //question id
echo  $idPregunta=$_POST['idPregunta'];
 echo $tipo=$_POST['tipo'];
 //do something 
 }
Karthikeyan Ganesan
  • 1,901
  • 20
  • 23