Select to load the elements when opening the page

0

I have a select that is dynamic, at the time of opening the page it performs its function but what I need is that when I open the page the default value is selected and I throw the images of the selected value, since it is annoying to open the page and not see anything until you select the value.

PHP:

include '../conexion.php';
$consulta = "SELECT * FROM mantras_productos ORDER BY tipo_producto ASC";
$query = mysqli_query($conexion,$consulta);

$producto.="<option value='0'>Selecciona Un Producto</option>";
while($f=mysqli_fetch_array($query,MYSQLI_ASSOC)){
    $producto.= "<option value='".$f['id_producto']."'>".$f['tipo_producto']."</option>";    
}
echo $producto;

Javascript:

$(nombreProdu());
function nombreProdu(busquedaProdu)
{
  $.ajax({
    url:'admin/phpadmin/selectProductos.php',
    type: 'GET',
    dataType:'html',
    data : {busquedaProdu: busquedaProdu},
  })

  .done(function(result){
    $("#cbx_carrito").html(result);
  })
}

$(document).ready(function(){     
    $("#cbx_carrito").change(function () {
        $("#cbx_carrito option:selected").each(function () {
            id_producto = $(this).val();
            $.get("php/carritoproductos.php", { id_producto: id_producto }, function(data){
                $("#cargaCarrito").html(data);
            });            
        });
    })
});
    
asked by jorge enrique chan castillo 05.09.2018 в 15:16
source

1 answer

0

The moment you prepare the options to show is when you can mark one as selected:

if (esProductoPorDefecto()) {
    $producto.= "<option selected value='".$f['id_producto']."'>".$f['tipo_producto']." /option>"; 
} else {
    $producto.= "<option value='".$f['id_producto']."'>".$f['tipo_producto']." /option>"; 
}

Where DefaultProduct () is the condition that you determine to select the default product, which may well be the first one on the list

    
answered by 05.09.2018 в 15:43