从复选框中选择多个值

时间:2013-06-28 09:18:18

标签: php checkbox

我想发送一个带有复选框ON的数组到upload.php。每个复选框都必须有一对从mysql列中提取的数据:CodigoCursoHorarioid。我无法将这些信息发送给upload.php。我怎么能这样做?

<?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 !";
}

2 个答案:

答案 0 :(得分:0)

更改

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

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

但请注意,如果未在html页面上勾选复选框,则不会向服务器发送任何值。您将需要测试是否设置了复选框名称,如果没有,则应对其应用值。或者更好的是,数据库字段应该具有默认值。

答案 1 :(得分:0)

您必须在复选框字段名称后添加方括号[]

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

请记住,如果未选中任何复选框,则不会发布该字段,因此您必须检查是否通过isset()功能设置。

  

重要:不要使用mysql_*函数,请改用PDO / mysqli。看看这里:Why shouldn't I use mysql_* functions in PHP?