-1

I have an issue with this form. I need to keep the values selected and I don't find the way. The thread where I posted the first time: Create php fom select multiple taking values from a table bd

The form:

<select name="fan[]" multiple="multiple"> <!-- Aquí se llama a la función verificarListaMultiple -->
    <?php
        foreach ($deportes as $aficion) {
            echo "<option value='".$aficion['idD']."'";
            if (isset($fan)) {
                verificarListaMultiple($aficion,$fan);
            }
            echo " >{$aficion['nombreDep']}</option>\n";
        }
    ?>            
</select>

And the function I'm trying to use:

function verificarListaMultiple($array, $valor) {
    if (in_array($valor, $array)) {
        echo 'selected = "selected"';
    }
}

Thanks.

albno
  • 9
  • 2

1 Answers1

0

Ok, i resolved on this way:

Form insert-data

<?php
    foreach ($deportes as $deporte)                         
        {
        echo "<option value='".$deporte['idD']."'";
        if (isset($depo)) verificarListaMultiple($depo, $deporte['idD']);
                            echo " >{$deporte['nombreDep']}</option>\n";
        }
?>  

Then, the values are inserted in a table bd:

<?php
if (isset($_POST['fan']))
    {
        $idPersona = mysqli_insert_id($conexion); 
        foreach ($depo as $sport) {
            $sport = mysqli_real_escape_string($conexion, $sport);
            $sql = "INSERT INTO mec(id,idD) VALUES ('$idPersona','$sport') ";
            $resul = mysqli_query($conexion, $sql);
}

And finally i would like to edit the form edit_Data. The values showed, must be the values inserted in form edit_Data, and must be keep selected:

Form edit-data:

<?php
foreach ($deportes as $deporte)                           
    {
    echo "<option value='".$deporte['idD']."'";
    if (isset($depo)) verificarListaMultiple($depo, $deporte['idD']);
                        echo " >{$deporte['nombreDep']}</option>\n";
    }
?>  
albno
  • 9
  • 2