0

I want to send an array with the checkboxs ON to upload.php. Every checkbox must have a pair of data extracted from mysql columns: CodigoCurso and Horarioid. I have trouble to send these information to upload.php. How can I perform this?

<?php
session_start();
$connection = mysql_connect("localhost", "root", "");
mysql_select_db("mydb", $connection);
$query = "SELECT Curso.CursoId, Hora.Horarioid,Persona.Cedula,Curso.CodigoCurso,Curso.NombreCurso,Curso.Creditos,Hora.Hora,Horario.PersonasMatriculadas FROM Persona INNER JOIN PreMatriculaEstudiante,Prematricula,Curso,Horario,Hora WHERE Persona.PersonaId = PreMatriculaEstudiante.PersonaId AND PreMatriculaEstudiante.PrematriculaID = Prematricula.PrematriculaId AND Prematricula.CursoId = Curso.CursoId AND Curso.CursoId = Horario.CursoId AND Horario.HorarioId = Hora.HorarioId AND Persona.Cedula = '" . $_SESSION["cedula"] . "'";
$result = mysql_query($query);

if ($row = mysql_fetch_array($result)) {
    echo "<form action='upload.php' method='POST'>";
    echo "<table border = '1'> \n";
    echo "<tr><td><b>Código Curso</b></td><td><b>Nombre Curso</b></td><td><b>Horario</b></td><td><b>Personas matriculadas</b></td></tr>";
    $k = 0;
    $t = 0;
    while ($array = mysql_fetch_array($result)) {
        echo "<tr>
                        <td>" . $array['CodigoCurso'] . "</td>" .
        "<td align='left'>" .
        "<input type='checkbox' name='info[$k][$k]' value='" . $array['CursoId'] . $array['Horarioid'] . "'.nonchecked>" . utf8_encode($array['NombreCurso']) . "</td>" .
        "<td>" . $array['Hora'] .
        "<td>" . $array['PersonasMatriculadas'] . "</td>" .
        "</tr>";

        $k++;
        $t++;
    }

    echo "</table> \n";
    echo "<p>";
    echo "<input type='submit' value='Ingresar datos'/>";
    echo "</p>";
    echo "</form>";
} else {
    echo "¡ No se ha encontrado ningún registro !";
}
jacoz
  • 3,508
  • 5
  • 26
  • 42
Chu
  • 468
  • 1
  • 5
  • 21

2 Answers2

0

Change

<input type='checkbox' name='info[$k][$k]' value=

to

<input type='checkbox' name='**{info[$k][$k]}[]**' value=

But be warned, if the checkbox is not ticked on the html page then no value is sent to the server. You will need to test if the checkbox name is set and if not the apply a value to it. Or better still, the database fields should have a default value.

0

You have to add square brackets [] after the checkbox field name:

"<input type='checkbox' name='info[$k][$k][]' value='" . $array['CursoId'] . $array['Horarioid'] . "'.nonchecked>" . utf8_encode($array['NombreCurso']) . "</td>" .

Remember that if no checkboxes are selected the field isn't posted, so you must check if is set via isset() function.

Important: Do not use mysql_* functions, use PDO/mysqli instead. Take a look here: Why shouldn't I use mysql_* functions in PHP?.

Community
  • 1
  • 1
jacoz
  • 3,508
  • 5
  • 26
  • 42
  • I get concatenate CursoId with Horarioid instead of they be on different "rows" in the array – Chu Jun 28 '13 at 09:39
  • Just a comment... why do you have the same index ? (Two `$k` in `info[$k][$k][]`, instead of `info[$k][$t][]` or `info[$t][$k][]` ? – jacoz Jun 28 '13 at 09:41
  • Hehe, first all, I'm a newbie on php. Second, I don't understand how data is allocate in arrays. My goal is: A checkbutton have two datas: CodigoCurso and Horarioid. When some checkboxes will be selected, I want these checkboxes with their CodigoCurso and Horarioid. I thought I must allocate at the same row on the array because are datas from the same checkbox – Chu Jun 28 '13 at 09:45
  • In your code, you're setting as checkbox value `CursoId` and `Horarioid` without any separator. – jacoz Jun 28 '13 at 09:47
  • I put a point between them. Like: .$array['CursoId'].$array['Horarioid']. – Chu Jun 28 '13 at 09:48
  • I did a var_dump(_$POST) on the file related with the form action, I get: array(1) { ["info"]=> array(2) { [0]=> array(1) { [0]=> array(1) { [0]=> string(4) "2613" } } [1]=> array(1) { [1]=> array(1) { [0]=> string(4) "2311" } } } } but 2613 must be CursoId->26 and Horarioid->13, the same with 2311: CursoId->23, Horarioid->11 – Chu Jun 28 '13 at 09:52
  • No! You're not separating it, you'are concatenating it. Try with: `. $array['CursoId'] . '.' . $array['Horarioid'] .` – jacoz Jun 28 '13 at 09:54
  • I expect something like: array(1) { ["info"]=> array(2) { [0]=> array(1) { [0]=> array(1) { [0]=> string(4) "26 [1]=>13" } } [1]=> array(1) { [1]=> array(1) { [0]=> string(4) "23 [1]=>11" } } } } – Chu Jun 28 '13 at 09:55
  • I have "".utf8_encode($array['NombreCurso'])."". And doesn't work – Chu Jun 28 '13 at 09:59
  • Try with: `"".utf8_encode($array['Nombre‌​Curso'])."".` – jacoz Jun 28 '13 at 10:14
  • I'm getting: array(1) { ["info"]=> array(2) { [0]=> array(1) { [0]=> array(1) { [0]=> string(5) "26.13" } } [5]=> array(1) { [5]=> array(1) { [0]=> string(5) "31.11" } } } }. Now are concatenated with a dot instead of insert a new element into the list, like [0]=> string(2) "31" [1]=>string(2) "11" – Chu Jun 28 '13 at 10:19