PHP - Memorizar las selecciones de un Select multiple

 
Vista:

Memorizar las selecciones de un Select multiple

Publicado por Marina (1 intervención) el 02/10/2020 21:41:54
Hola, Buenas,

No consiguo encontrar en la web como se memorizan las opciones seleccionadas en un select multiple alimentado con objetos procedientes de mi base de datos. Me refiero a cuando uno valida un formulario pero que le faltan campos por completar, etc... No quiero que se pierda la información y que el usario tenga que rellenarlo todo de nuevo.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<select name="product_attributes[]" id="select_attribute"  class="form-control" data-selectator-keep-open="true"  style="max-height: 32px"  multiple>
 
<?php
//$selected = isset( $_POST['product_attributes'] ) ? $_POST['product_attributes'] : "" ;
//$selectedValeur = 'selected="selected"';
$select_attribute = $_POST['product_attributes'];
foreach ( $select_attribute as $v ) { ?>
 <option value="<?php echo $v->attribute_id; ?>" <?php selected( true, in_array($v->attribute_id, $selected ) ); ?>><?php echo $v->attribute_title; ?></option><?php } ?>
 
 
 <?php   // Esta parte es ok
 $get_attribute = mysqli_query($con,"select * from product_attributes order by attribute_title");
 
 while ($attributes=mysqli_fetch_array($get_attribute))
          {
            $attribute_id = htmlspecialchars($attributes['attribute_id']);
            $attribute_title = htmlspecialchars($attributes['attribute_title']);
            echo "<option value='$attribute_id'> $attribute_title </option>";
           }
  ?>
  </select>

Gracias por la ayuda
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder